SQL Query To Obtain Value that Occurs more than once

前端 未结 7 2035
陌清茗
陌清茗 2020-12-08 19:26

I need to query my database to show the records inside my table where lastname occurs more than three times. Example: in my Students Table, there are 3 people with Lastname

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 19:32

    The answers mentioned here is quite elegant https://stackoverflow.com/a/6095776/1869562 but upon testing, I realize it only returns the last name. What if you want to return the entire record itself ? Do this (For Mysql)

    SELECT *
    FROM `beneficiary`
    WHERE `lastname`
    IN (
    
      SELECT `lastname`
      FROM `beneficiary`
      GROUP BY `lastname`
      HAVING COUNT( `lastname` ) >1
    )
    

提交回复
热议问题