gpt4 book ai didi

mysql - SQL:对行进行计数和分组

转载 作者:行者123 更新时间:2023-11-29 13:32:51 25 4
gpt4 key购买 nike

id | userid | exerciseid | date                | time | weight | distance | reps
1 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
2 | 24 | 24 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
3 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL
4 | 24 | 1 | 2000-00-00 00:00:00 | NULL | 100 | NULL | 2
5 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL

第 1、3 和 5 行相同。我想做一个将它们分组在一起的计数,同时还添加一个包含计数值的列。

SELECT id, userid, exerciseid, date, time, weight, distance, reps    
FROM `exercises`
WHERE `userid` = 1 AND `date` < now()

所以我希望它也返回类似的东西:

id | userid | exerciseid | date                | time | weight | distance | reps | count
1 | 24 | 1 | 2013-09-28 00:00:00 | 2321 | 231 | 121 | NULL | 3
4 | 24 | 1 | 2000-00-00 00:00:00 | NULL | 100 | NULL | 2 | NULL

最佳答案

试试这个

SELECT id, userid, exerciseid, date, time, weight, distance, reps,
count(*) as count
FROM `exercises`
WHERE userid = 1 AND date < now()
GROUP BY id, userid, exerciseid, date, time, weight, distance, reps

关于mysql - SQL:对行进行计数和分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19163901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com