计算 SELECT 语句中列值的总和
我有一个表
,我应该在其中计算用户是否达到了 3 点之类的处罚数量,因此,如果用户达到或超过 3 分,他就会被禁止:
table_warnings
- id
- user_id
- warning
- warning_date
id user_id warning warning_date
1 5478 1 2010-02-25 12:59:00
2 5478 1 2010-02-28 08:27:00
3 5478 2 2010-03-01 22:44:11
我已经 开始了这个问题,其中用户 KM 帮助我完善了 SELECT
的解决方案,如下所示
SELECT COUNT(warning)
FROM table_warnings AS w
INNER JOIN table_users AS u
ON w.user_id = u.user_id
WHERE w.user_id = 5478 AND warning_date >= '2010-02-05 12:59:00'
HAVING COUNT(warning) >= 3
:我找到了一种方法,可以让 SELECT
检索用户在过去 30 天内获得了多少罚分?
I have a table
where I should count if an user has reached a number of penalities like 3, so if the user reach or surpass 3 points, he's banned:
table_warnings
- id
- user_id
- warning
- warning_date
id user_id warning warning_date
1 5478 1 2010-02-25 12:59:00
2 5478 1 2010-02-28 08:27:00
3 5478 2 2010-03-01 22:44:11
I've started this question, where the user KM helped me to fine a solution of a SELECT
like this one:
SELECT COUNT(warning)
FROM table_warnings AS w
INNER JOIN table_users AS u
ON w.user_id = u.user_id
WHERE w.user_id = 5478 AND warning_date >= '2010-02-05 12:59:00'
HAVING COUNT(warning) >= 3
Can I find a way for a SELECT
to retrieve how many penality points the user has got in the last 30 days?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是“warn_penalty”列。我可以假设它与“警告”相同吗?如果是这样,只需将 COUNT() 更改为 SUM() 即可。通过一些额外的修复来消除第二个表:
将返回所有被禁止的用户。
或者,如果您有 user_id 并希望知道它是否被禁止:
并检查结果。
What is the column "warn_penalty". May I assume it's the same as "warning"? If so, simply change COUNT() to SUM(). With some additional fixing to eliminate the second table:
will return all users who are banned.
Alternatively, if you have a user_id and wish to know whether it is banned or not:
and check the result.