selecting rows which have occurred more than three times

后端 未结 2 685
野趣味
野趣味 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.

提交回复
热议问题