将内连接添加到查询中,使其成为3个表。

huangapple go评论59阅读模式
英文:

Adding inner join to query making it 3 tables

问题

我有一个正常运行的查询:

SELECT 
    user_id,
    COUNT() AS number_of_stuff
FROM 
    t2
WHERE
    EXISTS (SELECT 1
            FROM t1
            WHERE content_id = t2.content_id)
GROUP BY 
    user_id
ORDER BY 
    number_of_stuff

目前它返回了 user_id 以及 user_id 在 t1 中出现的次数。

我想要从 t3 中获取 user_name,条件是 t2.user_id = t3.user_id。如何实现?

英文:

I have a working query:

SELECT 
    user_id,
    COUNT() AS number_of_stuff
FROM 
    t2
WHERE
    EXISTS (SELECT 1
		    FROM t1
		    WHERE content_id = t2.content_id)
GROUP BY 
    user_id
ORDER BY 
    number_of_stuff

Currently it returns user_id and counts of the number of times user_id is in t1.

I want to get user_name from t3 where t2.user_id = t3.user_id. How do I do that?

答案1

得分: 0

你似乎描述了一个简单的JOIN

select t2.user_id, t3.user_name, count(*) as number_of_stuff
from t2 join
     t3
     on t2.user_id = t3.user_id
where exists (select 1 from t1 where t1.content_id=t2.content_id)
group by t2.user_id, t3.user_name
order by number_of_stuff;
英文:

You seem to have described a simple JOIN:

select t2.user_id, t3.user_name, count(*) as number_of_stuff
from t2 join
     t3
     on t2.user_id = t3.user_id
where exists (select 1 from t1 where t1.content_id=t2.content_id)
group by t2.user_id, t3.user_name
order by number_of_stuff;

答案2

得分: 0

这将完成它

选择 user_name、user_id、count() 作为 number_of_stuff 
从 t2 内连接
t3 其中 t2.user_id = t3.user_id
存在 (选择 1 从 t1 其中 content_id=t2.content_id) 
按 user_name、user_id 分组 按 number_of_stuff 排序
英文:

This will do it

select user_name, user_id, count() as number_of_stuff 
from t2 inner join
t3 where t2.user_id = t3.user_id
where exists (select 1 from t1 where content_id=t2.content_id) 
group by user_name , user_id order by number_of_stuf

huangapple
  • 本文由 发表于 2020年1月7日 02:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定