我现在数据表里有10个字段,装的都是车站信息,数据如下:id    cc            z1           z2            z3            z4            z5          z6           z7         z8        z9        z10
1   G101      北京  石家庄    唐山   葫芦岛   锦州   沈阳   铁岭
2   G102      大连   鞍山     辽阳    沈阳
我现在想做的查询语句是:当我查询id=1时只检索出前7列,因为Z8没有数据。
当查询id=2时,只检索到Z4,z5没有数据。这个SQL语句怎么写?

解决方案 »

  1.   

    select z1 as z from table where id = 1 union
    select z2 as z from table where id = 1 union
    ....
    select z10 as z from table where id = 1 union
      

  2.   

    额 最后一个union 去掉select z1 as z from table where id = 1 union
    select z2 as z from table where id = 1 union
    ....
    select z10 as z from table where id = 1;
      

  3.   

    需用动态SQL或行列转换,
    建议整行读取,后交给前端程序处理即可.