某表
若干字段  存的都是数字 一个字段是日期,即每一行记录的所有数字都属于这个日期的a--------------b----------c---------d---------e-----------f-------date
78-------------50---------70--------7890------782---------8-------2009-10-20
查询结果要变成
2009-10-20------ 78
2009-10-20------ 50
2009-10-20------ 70
2009-10-20------ 7890
2009-10-20------ 782
2009-10-20------ 8

解决方案 »

  1.   

    select `date`,a as num
    union all
    select `date`,b
    union all
    select `date`,c
    union all
    select `date`,d
    union all
    select `date`,e
    union all
    select `date`,f
    order by `date`;
      

  2.   

    漏了表名:select `date`,a as num from 表t
    union all
    select `date`,b from 表t
    union all
    select `date`,c from 表t
    union all
    select `date`,d from 表t
    union all
    select `date`,e from 表t
    union all
    select `date`,f from 表t
    order by `date`;
      

  3.   

    要加 where 的话  得每个from table 后面都重复加?
      

  4.   

    如果是日期的话,可以用子查询来加where条件
    select * 
    from(
    select `date`,a as num from 表t
    union all
    select `date`,b from 表t
    union all
    select `date`,c from 表t
    union all
    select `date`,d from 表t
    union all
    select `date`,e from 表t
    union all
    select `date`,f from 表t
    ) t
    where `date`=xxx
    order by `date`但如果是其他数字列的话,要每个都加上条件
      

  5.   


    建议在每个后加WHERER这样速度会快一些。
      

  6.   

    典型的行专列unionmysql> select * from user;
    +------+--------+
    | id   | userid |
    +------+--------+
    |    1 |      3 |
    |    2 |      3 |
    |    3 |      3 |
    |    1 |      3 |
    +------+--------+
    4 rows in set (0.00 sec)mysql> select group_concat(id) from user group by userid;
    +------------------+
    | group_concat(id) |
    +------------------+
    | 1,2,3,1          |
    +------------------+
    1 row in set (0.00 sec)典型的列转行