如何在 group_concat() 中使用 sum()?
问题修订
确实想要一个总和的 group_concat...
表:shops
+---------+--------+--------+
| shop_id | name | state |
+---------+--------+--------+
| 0 | shop 0 | 5 |
| 1 | shop 1 | 5 |
| 2 | shop 2 | 5 |
| 3 | shop 3 | 2 |
+---------+--------+--------+
表:项目
+------------+--------------+
| shop | item | quantity |
+------------+--------------+
| 0 | 0 | 1 |
| 0 | 1 | 2 |
| 0 | 2 | 3 |
| 1 | 0 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 0 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
| 3 | 1 | 2 |
| 3 | 2 | 3 |
+------------+--------------+
SELECT state,SUM(i.quantity) total
FROM shops s2
LEFT JOIN items i ON i.shop=s2.shopid
WHERE state=5
GROUP by item
result #1:
+--------+---------+
| state | total |
+--------+---------+
| 5 | 3 |
+--------+---------+
| 5 | 6 |
+--------+---------+
| 5 | 9 |
+--------+---------+
But I would like the totals, like this:
result #2:
+--------+---------+---------+----------+
| state | total 0 | total 1 | total 2 |
+--------+---------+---------+----------+
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
or using group_concat()
result #3
+--------+---------+
| state | totals |
+--------+---------+
| 5 | 3,6,9 |
+--------+---------+
我似乎无法让 group_concat 获取结果中的总计列 #1
提前致谢
Question revised
Really wanted a group_concat of sums...
Table: shops
+---------+--------+--------+
| shop_id | name | state |
+---------+--------+--------+
| 0 | shop 0 | 5 |
| 1 | shop 1 | 5 |
| 2 | shop 2 | 5 |
| 3 | shop 3 | 2 |
+---------+--------+--------+
Table: items
+------------+--------------+
| shop | item | quantity |
+------------+--------------+
| 0 | 0 | 1 |
| 0 | 1 | 2 |
| 0 | 2 | 3 |
| 1 | 0 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 0 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
| 3 | 1 | 2 |
| 3 | 2 | 3 |
+------------+--------------+
SELECT state,SUM(i.quantity) total
FROM shops s2
LEFT JOIN items i ON i.shop=s2.shopid
WHERE state=5
GROUP by item
result #1:
+--------+---------+
| state | total |
+--------+---------+
| 5 | 3 |
+--------+---------+
| 5 | 6 |
+--------+---------+
| 5 | 9 |
+--------+---------+
But I would like the totals, like this:
result #2:
+--------+---------+---------+----------+
| state | total 0 | total 1 | total 2 |
+--------+---------+---------+----------+
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
or using group_concat()
result #3
+--------+---------+
| state | totals |
+--------+---------+
| 5 | 3,6,9 |
+--------+---------+
I cannot seem to get group_concat to grab the total column in result #1
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改:
至
——
Change:
To
--
找到了一种方法来做到这一点:
Found a way to do this :
据我所知你不能在 MySQL 中做到这一点。动态列仅支持 group_contcat() 的范围,它仍然将多个结果行聚合到单个列中。
仅当您有固定/有限数量的
Total X
-s 时,您才可以在查询中明确声明它们本身。As far as I know you can't do that in MySQL. Dynamic columns are only supported to the extent of group_contcat() which still aggregates multiple results rows into a single column.
Only if you have a fixed/limited number of
Total X
-s, you can declare them in the query explicitely per se.