selecting rows which have occurred more than three times

后端 未结 2 684
野趣味
野趣味 2021-01-17 07:36

I have a table active_users and from that i want to select the rows where user_id has occurred more than 2 times and notified = 0 .Want single result only

相关标签:
2条回答
  • 2021-01-17 07:38

    Just count and group by user_id (and any other optional properties), together with having clause:

    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
    

    This will give you all users occurred 3 times and more into the table.

    0 讨论(0)
  • 2021-01-17 07:40
    SELECT *
    FROM active_users
    WHERE notified = 0
    GROUP BY user_id
    HAVING COUNT(id) >2
    
    0 讨论(0)
提交回复
热议问题