group_concat 在 php 中不工作
group_concat is not working in php
我关注mysql table:
table: 出价
id | compid | itemid | rate | year
1 | 1 | 1 | 2.0 | 2015
2 | 1 | 2 | 4.2 | 2015
3 | 1 | 3 | 2.3 | 2015
4 | 2 | 1 | 3.0 | 2015
5 | 2 | 2 | 4.0 | 2015
6 | 2 | 3 | 2.5 | 2015
7 | 3 | 1 | 2.3 | 2015
8 | 3 | 2 | 4.5 | 2015
9 | 3 | 3 | 2.8 | 2015
如果我使用来自 php 的以下 mysql 查询,效果很好:
SELECT itemid, GROUP_CONCAT(rate) FROM bid GROUP BY itemid
结果:
Array ( [itemid] => 1 [GROUP_CONCAT(rate)] => 2.0,3.0,2.3 )
Array ( [itemid] => 2 [GROUP_CONCAT(rate)] => 4.2,4.0,4.5 )
Array ( [itemid] => 3 [GROUP_CONCAT(rate)] => 2.3,2.5,2.8 )
但是如果我使用以下查询,它就不起作用:
SELECT itemid, GROUP_CONCAT(CASE WHEN compid=1 THEN rate ELSE 0 END as company01, CASE WHEN compid=2 THEN rate ELSE 0 END as company02, CASE WHEN compid=3 THEN rate ELSE 0 END as company03) FROM bid GROUP BY itemid
我的第二个 SELECT 查询有什么错误,如何实现 GROUP_CONCAT?
如果你想制作列,也许这个查询。
SELECT itemid,
MAX(CASE WHEN compid = 1 THEN rate END) as 'company01',
MAX(CASE WHEN compid = 2 THEN rate END) as 'company02',
MAX(CASE WHEN compid = 3 THEN rate END) as 'company03'
FROM bid
GROUP BY itemid
我关注mysql table: table: 出价
id | compid | itemid | rate | year
1 | 1 | 1 | 2.0 | 2015
2 | 1 | 2 | 4.2 | 2015
3 | 1 | 3 | 2.3 | 2015
4 | 2 | 1 | 3.0 | 2015
5 | 2 | 2 | 4.0 | 2015
6 | 2 | 3 | 2.5 | 2015
7 | 3 | 1 | 2.3 | 2015
8 | 3 | 2 | 4.5 | 2015
9 | 3 | 3 | 2.8 | 2015
如果我使用来自 php 的以下 mysql 查询,效果很好:
SELECT itemid, GROUP_CONCAT(rate) FROM bid GROUP BY itemid
结果:
Array ( [itemid] => 1 [GROUP_CONCAT(rate)] => 2.0,3.0,2.3 )
Array ( [itemid] => 2 [GROUP_CONCAT(rate)] => 4.2,4.0,4.5 )
Array ( [itemid] => 3 [GROUP_CONCAT(rate)] => 2.3,2.5,2.8 )
但是如果我使用以下查询,它就不起作用:
SELECT itemid, GROUP_CONCAT(CASE WHEN compid=1 THEN rate ELSE 0 END as company01, CASE WHEN compid=2 THEN rate ELSE 0 END as company02, CASE WHEN compid=3 THEN rate ELSE 0 END as company03) FROM bid GROUP BY itemid
我的第二个 SELECT 查询有什么错误,如何实现 GROUP_CONCAT?
如果你想制作列,也许这个查询。
SELECT itemid,
MAX(CASE WHEN compid = 1 THEN rate END) as 'company01',
MAX(CASE WHEN compid = 2 THEN rate END) as 'company02',
MAX(CASE WHEN compid = 3 THEN rate END) as 'company03'
FROM bid
GROUP BY itemid