本帖最后由 elvaone 于 2011-07-05 16:21:20 编辑

解决方案 »

  1.   

    select a1,count(*),count(*)/
    (select count(*) from 
    (select a1 from tt
    union
    select a2 from tt
    ...
    union
    select a30 from tt) a) from (
    select a1 from tt
    union
    select a2 from tt
    ...
    union
    select a30 from tt) a
    group by a1
      

  2.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    CREATE TABLE `vote` (
      `a1` char(1) default NULL,
      `a2` char(1) default NULL,
      ......
      `a29` char(1) default NULL,
      `a30` char(1) default NULL,
      `time` char(100) default NULL,
      `id` int(50) unsigned NOT NULL auto_increment,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;a1--a30中存储的都是数字,1,2,3,4.。。13==但不确定而且不知道最大值。
    需要的结果是:
    a1到a30每个字段中出现的每个数字的次数和在本表数据条数中的百分比(有100条数据,那么分母就是100)这样够清楚了吧。
      

  4.   

    Select n,count(*),count(*)/(select count(*)*30 from vote)
    From (
    Select a1 as n From vote
     Union all
    Select a2 From vote
     Union all
    Select a3 From vote
     Union all
    Select a4 From vote
     Union all
    Select a5 From vote
     Union all
    Select a6 From vote
     Union all
    Select a7 From vote
     Union all
    Select a8 From vote
     Union all
    Select a9 From vote
     Union all
    Select a10 From vote
     Union all
    Select a11 From vote
     Union all
    Select a12 From vote
     Union all
    Select a13 From vote
     Union all
    Select a14 From vote
     Union all
    Select a15 From vote
     Union all
    Select a16 From vote
     Union all
    Select a17 From vote
     Union all
    Select a18 From vote
     Union all
    Select a19 From vote
     Union all
    Select a20 From vote
     Union all
    Select a21 From vote
     Union all
    Select a22 From vote
     Union all
    Select a23 From vote
     Union all
    Select a24 From vote
     Union all
    Select a25 From vote
     Union all
    Select a26 From vote
     Union all
    Select a27 From vote
     Union all
    Select a28 From vote
     Union all
    Select a29 From vote
     Union all
    Select a30 From vote
    ) t
    Group by n
      

  5.   

    学习一下。我还没写过这么长的sql语句呢