group_concat is not working in php

Abdullah Mamun-Ur- Rashid

I have following mysql table: table: bid

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

If I use following mysql query from php, it works well:

SELECT itemid, GROUP_CONCAT(rate) FROM bid GROUP BY itemid

Result:

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 )   

But if I use following query, it is not working:

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

What is error in my second SELECT query and how to achieve GROUP_CONCAT by this?

check

If you want to make columns, maybe this query.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related