[英]While using a SELECT SUM() GROUP BY, how can I still get the individual values?
values = 2,3,4
When doing a JOIN (SELECT *, SUM(values) AS MaxValues FROM table GROUP BY id)
, $a = $data['MaxValues']
will equal 9 as it should. 当执行
JOIN (SELECT *, SUM(values) AS MaxValues FROM table GROUP BY id)
, $a = $data['MaxValues']
将等于9。 However, how can I still pull the individual values(2,3,4)? 但是,如何仍然可以提取各个值(2,3,4)? trying a foreach on
$b = $data['values']
only gives me one of the values. 在
$b = $data['values']
上尝试foreach仅给我一个值。 I'm assuming because of the required GROUP BY
. 我假设是因为需要
GROUP BY
。 I'm still new to all of this. 我还是所有这一切的新手。 Thank you
谢谢
You can select both values
and SUM
with JOIN
. 您可以使用
JOIN
选择values
和SUM
。 Also, you don't need GROUP BY
if you want to get all the values, eg: 另外,如果要获取所有值,则不需要
GROUP BY
,例如:
SELECT value, a.sum
FROM table, (SELECT SUM(value) AS `sum` FROM table ) a;
You are looking for group_concat()
, I think: 您正在寻找
group_concat()
,我认为:
JOIN (SELECT id, SUM(values) AS MaxValues, group_concat(values) as values
FROM table
GROUP BY id)
Note: You should not be using SELECT *
with GROUP BY
. 注意:不应将
SELECT *
和GROUP BY
一起使用。 Only include the columns in the GROUP BY
clause. 仅在
GROUP BY
子句中包括列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.