gpt4 book ai didi

mysql - 如何在单个查询中从多个表中获取数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:10 25 4
gpt4 key购买 nike

我有以下表格

包含列名称(UserId、姓名、电子邮件)的用户表

带有列名(UserId、VideoLink)的视频表

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

我想得到如下结果

姓名Total_Videos_posted_by_that_usercount(Likes)count(dislikes)

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

最佳答案

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 demo

关于mysql - 如何在单个查询中从多个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com