views:

67

answers:

1

Hi, 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?

+5  A: 

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.

Larry Lustig
exactly, thanks for help
Vittorio Vittori