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

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

Adding inner join to query making it 3 tables

问题

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

  1. SELECT
  2. user_id,
  3. COUNT() AS number_of_stuff
  4. FROM
  5. t2
  6. WHERE
  7. EXISTS (SELECT 1
  8. FROM t1
  9. WHERE content_id = t2.content_id)
  10. GROUP BY
  11. user_id
  12. ORDER BY
  13. number_of_stuff

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

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

英文:

I have a working query:

  1. SELECT
  2. user_id,
  3. COUNT() AS number_of_stuff
  4. FROM
  5. t2
  6. WHERE
  7. EXISTS (SELECT 1
  8. FROM t1
  9. WHERE content_id = t2.content_id)
  10. GROUP BY
  11. user_id
  12. ORDER BY
  13. 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

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

You seem to have described a simple JOIN:

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

答案2

得分: 0

这将完成它

  1. 选择 user_nameuser_idcount() 作为 number_of_stuff
  2. t2 内连接
  3. t3 其中 t2.user_id = t3.user_id
  4. 存在 (选择 1 t1 其中 content_id=t2.content_id)
  5. user_nameuser_id 分组 number_of_stuff 排序
英文:

This will do it

  1. select user_name, user_id, count() as number_of_stuff
  2. from t2 inner join
  3. t3 where t2.user_id = t3.user_id
  4. where exists (select 1 from t1 where content_id=t2.content_id)
  5. 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:

确定