mysql group_concat()和sum()

发布于 2025-01-22 16:52:24 字数 608 浏览 4 评论 0原文

例如,我有下表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2025-01-29 16:52:25

几分钟的谷歌搜索可以提供此文章

SELECT id, type, SUM(credit)
FROM table
GROUP BY type;

将所有等同类型组合在一起。如果您只想获得独特的类型,请从表中选择独特的类型

A few minutes of googling would have provided this article

SELECT id, type, SUM(credit)
FROM table
GROUP BY type;

Group by will group all equal types together. If you just want to get the distinct type use SELECT DISTINCT type from table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文