drktvjp713333 2015-02-24 19:57 采纳率: 0%
浏览 65
已采纳

group_concat在php中不起作用

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?

  • 写回答

1条回答 默认 最新

  • doubei8168 2015-02-24 20:40
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 QGC打开没有地图显示,离线地图也不显示,如何解决?
  • ¥20 Android Studio 的 webview 与访问网络存在的限制
  • ¥15 某帖子的数据集不清楚来源,求帮助
  • ¥50 tc358743xbg寄存器配置
  • ¥15 idea构建mod报错无效的源发行版项目链接,如何解决?
  • ¥15 springboot中的路径问题
  • ¥80 App Store Connect 中设置了订阅项目,Xcode 中预览可以正确显示价格,真机测试却无法显示
  • ¥15 MATLAB的PIV算法问题
  • ¥15 RflySim例程学习:matlab编译报错
  • ¥20 谁来给我解答一下疑惑
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部