选择包含更多项目的用户

col*_*rco 0 sql

每个用户都有很多照片和很多评论.我想订购用户

SUM(number_of_photos, number_of_comments)

你能建议我的SQL查询吗?

Ale*_*lli 5

带有JOIN的GROUP BY比依赖子查询更有效(在我所知的所有关系数据库中):

Select * From Users
Left Join Photos On (Photos.user_id = Users.id)
Left Join Comments On (Comments.user_id = Users.id)
Group By UserId
Order By (Count(Photos.id) + Count(Comments.id))
Run Code Online (Sandbox Code Playgroud)

对表有一些假设(例如id每个表中的主键).