mysql group_concat()和sum()
例如,我有下表:
id type credit
1 loan1 300
2 loan1 200
3 loan2 500
4 loan2 300
5 allowance1 400
6 allowance2 300
到目前为止,我想在这样的一行中显示它们
id loan
1 loan1: 500, loan2: 800
,我有以下查询以单行
GROUP_CONCAT(CASE WHEN type LIKE 'loan%' THEN concat(type, ': ', credit) ELSE NULL END SEPARATOR ', ') as loan
输出显示它们:
id loan
1 loan1: 300, loan1: 200, loan2: 500, loan2: 300
如何仅显示不同的类型并总结它们的信用?
For example, I have the following table:
id type credit
1 loan1 300
2 loan1 200
3 loan2 500
4 loan2 300
5 allowance1 400
6 allowance2 300
I want to display them in a single row like this
id loan
1 loan1: 500, loan2: 800
so far, I have the following query which displays them in a single row
GROUP_CONCAT(CASE WHEN type LIKE 'loan%' THEN concat(type, ': ', credit) ELSE NULL END SEPARATOR ', ') as loan
output:
id loan
1 loan1: 300, loan1: 200, loan2: 500, loan2: 300
How do I display only distinct types and sum their credit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几分钟的谷歌搜索可以提供此文章
将所有等同类型组合在一起。如果您只想获得独特的类型,请从表中选择独特的类型
A few minutes of googling would have provided this article
Group by will group all equal types together. If you just want to get the distinct type use SELECT DISTINCT type from table