[英]group_concat concat sum if dates are equal
I have this group_concat with concat that is working fine except I need to find a way to get the same results but sum(pest_count) where scout_date is the same. 我有这个带有concat的group_concat,它工作正常,除了我需要找到一种方法来获得相同的结果,但是sum(pest_count)的地方scout_date相同。 The $scout_date is coming from a dropdown that is looking at the same table for the dates so it has the format 2014-03-13.
$ scout_date来自一个下拉列表,该下拉列表在同一表中查找日期,因此其格式为2014-03-13。 Thanks for looking.
感谢您的光临。
"SELECT group_concat(concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>') SEPARATOR ' ') pests,
card_id,
card_type,
top_y,
left_x,
scout_date FROM scout_logpestnum WHERE custnum='$custnum' AND locationname='$locationname' AND scoutlogname = '$scoutlogname' AND date(scout_date) ='$scout_date'
GROUP BY
card_id,
card_type,
top_y,
left_x,
scout_date"
Here's my object that echos the result 这是我的对象,呼应结果
<?php echo $obj['pests']; ?>
Here's my table data 这是我的表数据
pest_name pest_count card_id card_type top_y left_x scout_date
Aphids 2 1 yellcard 652 703 2014-3-13 15:59:54
Thrips 4 1 yellcard 652 703 2014-3-13 15:59:54
Thrips 2 2 bluecard 754 707 2014-3-13 15:59:54
Thrips 1 3 yellcard 531 616 2014-3-13 15:59:54
Thrips 1 5 yellcard 80 613 2014-3-13 15:59:54
Aphids 1 1 yellcard 652 703 2014-3-13 16:00:04
Thrips 2 1 yellcard 652 703 2014-3-13 16:00:04
Here's what the query will return now for card_id 1 查询现在将返回card_id 1的内容
2 Aphids
4 Thrips
1 Aphids
2 Thrips
Here' the desired result 这是理想的结果
Here's what I want in the end for each pest_name and pest_count at each card. 这是我最后想要的每张卡上的每个pest_name和pest_count的内容。 This would be card_id 1 for the date 2014-3-13
这将是2014-3-13日期的card_id 1
3 Aphids
6 Thrips
Here's my final version for now thanks to John for the much needed help. 这是我现在的最终版本,感谢John急需的帮助。
SELECT card_id,left_x,top_y,card_type,
GROUP_CONCAT(CONCAT('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>') SEPARATOR ' ') pests FROM(
SELECT card_id,left_x,top_y,card_type, pest_name AS pest_name, sum(pest_count) AS pest_count, scout_date
FROM scout_logpestnumtest
GROUP BY pest_name,card_id, date(scout_date)
) as t
GROUP by card_id
I believe what you want to do is group by the DATE of the datetime field instead of including the timestamp.. when you do DATE(scout_date)
it will ignore the timestamp and just group the date.. so since thats the only field that is different that would cause the grouping to show 4 instead of 2 results it seems like thats the problem there. 我相信您想要做的是按datetime字段的DATE分组,而不是包括时间戳..当您执行
DATE(scout_date)
,它将忽略时间戳,而仅将日期分组..因为那是唯一的字段不同的将导致分组显示4个结果而不是2个结果,这似乎就是问题所在。
SELECT
group_concat(
concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>') SEPARATOR ' '
) pests,
card_id,
card_type,
top_y,
left_x,
scout_date
FROM scout_logpestnum
WHERE custnum='$custnum'
AND locationname='$locationname'
AND scoutlogname = '$scoutlogname'
AND date(scout_date) ='$scout_date'
GROUP BY card_id, DATE(scout_date)
EDIT : my recommendation is to sum the data first then do the concat afterwards like this. 编辑 :我的建议是首先对数据求和,然后再进行连接。 see FIDDLE for example
例如参见FIDDLE
SELECT
card_id,
group_concat(
concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>') SEPARATOR ' '
) pests
FROM(
SELECT
pest_name AS pest_name,
sum(pest_count) AS pest_count,
card_id,
card_type,
top_y,
left_x,
scout_date
FROM scout_logpestnum
WHERE custnum='$custnum'
AND locationname='$locationname'
AND scoutlogname = '$scoutlogname'
AND date(scout_date) ='$scout_date'
GROUP BY card_id, card_type, top_y, left_x, date(scout_date)
) as t
GROUP BY card_id
EDIT: this FIDDLE shows the data by card id.. so my edit should work with the above query.. I removed the group concat just so you can read it better... it groups by the card and the name and the date so that way you have the correct number of div
's returned. 编辑:此字段按卡ID显示数据。因此,我的编辑应适用于以上查询。.我删除了concat组,以便您可以更好地阅读...按卡以及名称和日期进行分组这样,您将获得正确数量的
div
返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.