0

我有以下表格

具有列名称(UserId、Name、email)的用户表

具有列名 (UserId,VideoLink) 的 Videos 表

如果 status=0 喜欢,如果 status=1 不喜欢,则包含列名称 (UserId,Status) 的 Activity 表

我想得到以下结果

姓名, Total_Videos_posted_by_that_user , count(喜欢) , count(dislikes)

我如何在单个查询中查询它们?

4

1 回答 1

3
select u.name, 
       videocount, 
       dislikes,
       likes
from user u
left join 
(
  select u.name, count(v.videolink) as videocount
  from user u
  inner join videos v on v.userid = u.userid
) x on x.name = u.name
left join 
(
  select u.name, 
         sum(a.status=1) as dislikes,
         sum(a.status=0) as likes
  from user u
  inner join activity a on a.userid = u.userid
) y on y.name = u.name

SQLFiddle 演示

于 2013-11-13T05:58:51.437 回答