SQL query at least one of something

前端 未结 6 691
鱼传尺愫
鱼传尺愫 2021-02-01 07:33

I have a bunch of Users, each of whom has many Posts. Schema:

Users: id
Posts: user_id, rating

How do I find all Users who have at least one po

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 08:07

    You can join the tables to find the relevant users, and use DISTINCT so each user is in the result set at most once even if they have multiple posts with rating > 10:

    select distinct u.id,u.username
    from users u inner join posts p on u.id = p.user_id 
    where p.rating > 10
    

提交回复
热议问题