问个sql基础问题? 有三个表User{Id,Name} Log{Id,UserId,content} Photo{Id,UserId,URL}
log表和photo表的UserId都是引用User表的Id怎么查询出:UserId,UserName,LogCount,PhotoCount?即用户Id,用户名,日志总...
log表和photo表的UserId都是引用User表的Id
怎么查询出:
UserId ,UserName,LogCount,PhotoCount ?即
用户Id,用户名,日志总数,相片总数 展开
怎么查询出:
UserId ,UserName,LogCount,PhotoCount ?即
用户Id,用户名,日志总数,相片总数 展开
3个回答
展开全部
select Id,Name,b.LogCount ,c.PhotoCount
from User a,
(select UserId,count(UserId) as LogCount from Log group by UserId) b,
(select UserId,count(UserId) as PhotoCount from Photo group by UserId) c
where a.Id=b.UserId(+) and a.Id=c.UserId(+)
from User a,
(select UserId,count(UserId) as LogCount from Log group by UserId) b,
(select UserId,count(UserId) as PhotoCount from Photo group by UserId) c
where a.Id=b.UserId(+) and a.Id=c.UserId(+)
追问
问个问题,把上面查出的结果按n条为一页分页,查出第m页怎么查?
追答
select Id,Name,b.LogCount ,c.PhotoCount from
(select rownum as num,Id,Name,b.LogCount ,c.PhotoCount
from User a,
(select UserId,count(UserId) as LogCount from Log group by UserId) b,
(select UserId,count(UserId) as PhotoCount from Photo group by UserId) c
where a.Id=b.UserId(+) and a.Id=c.UserId(+) ) t
where t.num between n*(m-1)+1 and n*m
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select a.Id,a.Name,Count(b.id),Count(c.id)
from user a,log b,photo c
where a.id=b.userid and a.id=c.userid
group by a.id,a.name
from user a,log b,photo c
where a.id=b.userid and a.id=c.userid
group by a.id,a.name
更多追问追答
追问
我测试了一下,你这个返回了一行数据,而且CountPhoto,countLog都不知道是什么怪数据
追答
select a.Id,a.Name,Count(b.id),Count(c.id)
from user a,log b,photo c,user d
where a.id=b.userid and d.id=c.userid
group by a.id,a.name
这个就对了,你测试
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select u.id,u.name,count(l.userid),count(p.userid) from user as u join log as l on u.id=l.userid join photo as p on u.id=p.userid group by l.userid,p.userid
追问
你的应该正确!
select a.Id,a.UserName,a.ImageFolderCount,count(b.Id) as LogCount from
(select UserT.Id,UserT.UserName,count(ImageFolder.Id) as ImageFolderCount from UserT
left join ImageFolder on ImageFolder.UserId=UserT.Id group by UserT.Id,UserT.UserName )as a
left join LogT as b on a.Id=b.UserId group by a.Id,a.UserName,a.ImageFolderCount
我也拼出来了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询