是否可以使用sql (mysql)来展平报表的数据库表。我可以用php来做,但如果我能用sql做得更简单就好了。
示例:
1)输入:原始数据-每个客户/开始和结束时间一行
cust start end
8000 0900 1000
8000 1000 1100
8000 1200 1300
9000 0900 1000
9000 1000 11002)所需输出:数据展平-每个客户一行,所有开始和结束时间都在同一行上
cust all related start-end times
---- ------------------------------
8000 0900-1000 1000-1100 1200-1300
9000 0900-1000 1000-1100 发布于 2013-05-08 02:14:43
这就是您要查找的内容:
select cust, group_concat(concat(start,'-',end) SEPARATOR ' ')
from mytable
group by cust请参阅working fiddle。
发布于 2013-05-08 02:16:09
SQL查询必须具有固定数量的列。这在您的情况下会带来问题,因为您不知道需要多少列。
一种解决方法是将所有结果放在一列中。这将产生类似于'090-1000,1000-1100,1200-1300‘的结果。您可以使用group_concat()实现这一点
select cust,
group_concat(concat(start, '-', end) separator ', ' order by start) as StartEnds
from t
group by custhttps://stackoverflow.com/questions/16425423
复制相似问题