英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论