我有一个表active_users,从那里我想选择user_id发生超过2次并且通知= 0的行。只有单个结果
需要查询结果
Id User_id Mobile last_seen notified
1 25853 XXXXXXXX 14-May-2016 17:11:12 0
答案 0 :(得分:1)
SELECT *
FROM active_users
WHERE notified = 0
GROUP BY user_id
HAVING COUNT(id) >2
答案 1 :(得分:1)
只需按user_id(以及任何其他可选属性)计算和分组,以及having子句:
SELECT
user_id,
mobile,
max(last_seen) AS last_seen,
notified,
count(user_id) AS number_of_records
FROM
active_users
WHERE
notified = 0
GROUP BY
user_id,
mobile,
notified
HAVING
count(user_id) > 2
这将使您所有用户在表格中出现3次以上。