作者jonce007 (汪汪)
看板Database
标题[SQL ] 语法求教,多栏位值如何有条件变成多笔row
时间Mon Sep 5 16:51:45 2016
资料库版本:mysql 5.x
内容/问题描述: 将不为空白的栏位们变成多笔资料
表格a内,有三组no&amt
其中第一组 no1,amt1必有资料,
第二及第三组可能有值也可能空白
table a
-------------------------------------
id date no1 amt1 no2 amt2 no3 amt3
-- ------ --- ---- --- ---- --- ----
1 160901 001 2 002 12
2 160902 010 3
3 160903 021 6 022 4 022 2
------------------------------------
如何能自己join自己展开成:
id date no amt
-- ------ --- ----
1 160901 001 2
1 160901 002 12
2 160902 010 3
3 160903 021 6
3 160903 022 4
3 160903 028 2
目前想不出来,转时用union替代
select id, date , no1 as no, amt1 as amt from a where ....
union
select id, date , no2 as no, amt2 as amt from a where ... and no2<>''
union
select id, date , no3 as no, amt3 as amt from a where ... and no3<>''
求SQL前辈先进们提示方向
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.216.136.100
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1473065508.A.D44.html
1F:→ meteor09887: 感觉你的问题用union就可以解了 09/06 12:26
2F:→ meteor09887: 只是我不太懂为什麽要<>'' 09/06 12:26
3F:→ jonce007: 空=没资料不必展开。union要扫table三次,希望一次就好 09/06 18:49