计算 SELECT 语句中列值的总和

发布于 2024-08-24 05:24:49 字数 782 浏览 2 评论 0原文

我有一个,我应该在其中计算用户是否达到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-08-31 05:24:49

什么是“warn_penalty”列。我可以假设它与“警告”相同吗?如果是这样,只需将 COUNT() 更改为 SUM() 即可。通过一些额外的修复来消除第二个表:

SELECT user_id, SUM(warning)
FROM table_warnings 
WHERE  warning_date >= '2010-02-05 12:59:00'
GROUP BY user_id
HAVING SUM(warning) >= 3

将返回所有被禁止的用户。

或者,如果您有 user_id 并希望知道它是否被禁止:

SELECT SUM(warning)
FROM table_warnings 
WHERE user_id = 5478 AND warning_date >= '2010-02-05 12:59:00'

并检查结果。

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:

SELECT user_id, SUM(warning)
FROM table_warnings 
WHERE  warning_date >= '2010-02-05 12:59:00'
GROUP BY user_id
HAVING SUM(warning) >= 3

will return all users who are banned.

Alternatively, if you have a user_id and wish to know whether it is banned or not:

SELECT SUM(warning)
FROM table_warnings 
WHERE user_id = 5478 AND warning_date >= '2010-02-05 12:59:00'

and check the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文