从第一张表中通过第二张表的两个ID获取行。

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

Get row from first table in many to many by two id from second table

问题

以下是翻译好的部分:

我有一个关于SQL查询的问题。我有3个表。
table_1

id name
1 aaa
2 bbb
3 ccc
4 ddd

table_2

id name
1 zzz
2 yyy
3 xxx
4 www

还有一个用于多对多关系的第三个表。

table_1_table_2

table_1_id table_2_id
1 1
1 2
3 1
3 2
3 3
3 4
4 4

最后,我的问题是。
例如,我有来自table_2的id(1和2);
我需要获取所有在t1_t2中具有t2.id = 1和2的table_1中的行。
如何获得这个结果:

table_1_id table_1_name
1 aaa
3 ccc

底部的代码不起作用

  1. SELECT * FROM `table_1`
  2. JOIN `table_1_table_2` ON table_1.id=table_1_table_2.table_1_id
  3. JOIN `table_2` ON table_2.id=table_1_table_2.table_2_id
  4. WHERE table_2.id = 1 OR table_2.id = 2

请注意,我在代码中的WHERE条件中使用了"OR"而不是"AND",因为您希望获取具有table_2.id为1或2的行。

英文:

i have a problem with sql request. I have 3 tables.
table_1

id name
1 aaa
2 bbb
3 ccc
4 ddd

table_2

id name
1 zzz
2 yyy
3 xxx
4 www

And third table for many to many relationship.

table_1_table_2

table_1_id table_2_id
1 1
1 2
3 1
3 2
3 3
3 4
4 4

And finally my question.
For example i have id from table_2 (1 and 2);
I need get all rows from table 1 which have row in t1_t2 where t2.id = 1 and 2
How to get this result:

table_1_id table_1_name
1 aaa
3 ccc

The code bottom is not working

  1. SELECT * FROM `table_1`
  2. JOIN `table_1_table_2` ON table_1.id=table_1_table_2.table_1_id
  3. JOIN `table_2` ON table_2.id=table_1_table_2.table_2
  4. WHERE table_2.id = 1 AND table_2.id = 2

答案1

得分: 0

尝试这个:

  1. SELECT t1.*
  2. FROM table1 t1
  3. JOIN table2_table1 tt ON tt.table1_id = t1.id
  4. JOIN table2 t2 ON tt.table2_id = t2.id
  5. WHERE t2.id = 'second_table_id1'
  6. AND t2.id = 'second_table_id2';
英文:

try this:

  1. SELECT t1.*
  2. FROM table1 t1
  3. JOIN table2_table1 tt ON tt.table1_id = t1.id
  4. JOIN table2 t2 ON tt.table2_id = t2.id
  5. WHERE t2.id = 'second_table_id1'
  6. AND t2.id = 'second_table_id2';

答案2

得分: 0

WHERE table_2.id = 1 AND table_2.id = 2
这不可能在同一列中有两个不同的id值
也许你的意思是:

WHERE table_1.id = 1 AND table_2.id = 2

英文:
  1. WHERE table_2.id = 1 AND table_2.id = 2

it is impossible to have a row with two different ids in the same column
perhaps you meant:

  1. WHERE table_1.id = 1 AND table_2.id = 2

答案3

得分: 0

以下是代码的翻译部分:

  1. 不需要连接所有表格。只需使用关系表格来计算每个id1id2数量,然后如果您想要收集id1名称,可以与tab1连接
  2. SELECT tab1.id,
  3. tab1.name
  4. FROM tab1
  5. INNER JOIN tab12
  6. ON tab1.id = tab12.table_1_id
  7. WHERE tab12.table_2_id IN (1,2)
  8. GROUP BY tab1.id,
  9. tab1.name
  10. HAVING COUNT(DISTINCT tab12.table_2_id) = 2

输出

id name
1 aaa
3 ccc

在此查看演示

英文:

You don't need to join all tables. It's sufficient to use the relationships table to count amount of id2 for each id1, then join with tab1 if you want to gather id1 names.

  1. SELECT tab1.id,
  2. tab1.name
  3. FROM tab1
  4. INNER JOIN tab12
  5. ON tab1.id = tab12.table_1_id
  6. WHERE tab12.table_2_id IN (1,2)
  7. GROUP BY tab1.id,
  8. tab1.name
  9. HAVING COUNT(DISTINCT tab12.table_2_id) = 2

Output:

id name
1 aaa
3 ccc

Check the demo here.

答案4

得分: 0

以下是您提供的代码的翻译部分:

  1. Others already answered, I just propose a slight different way:
  2. 其他人已经回答了,我只是提出一个稍微不同的方法:
  3. Example data:
  4. 示例数据:
  5. CREATE TABLE t1(
  6. id INT,
  7. name VARCHAR(30)
  8. );
  9. 创建表 t1
  10. INSERT INTO t1 VALUES
  11. (1, 'aaa'),
  12. (2, 'bbb'),
  13. (3, 'ccc'),
  14. (4, 'ddd');
  15. 将数据插入表 t1
  16. CREATE TABLE t2(
  17. id INT,
  18. name VARCHAR(30)
  19. );
  20. 创建表 t2
  21. INSERT INTO t2 VALUES
  22. (1, 'zzz'),
  23. (2, 'yyy'),
  24. (3, 'xxx'),
  25. (4, 'www');
  26. 将数据插入表 t2
  27. CREATE TABLE t1_2(
  28. t1_id INT,
  29. t2_id INT
  30. );
  31. 创建表 t1_2
  32. INSERT INTO t1_2 VALUES
  33. (1, 1),
  34. (1, 2),
  35. (3, 3),
  36. (4, 4);
  37. 将数据插入表 t1_2
  38. Query:
  39. 查询:
  40. SELECT id, name
  41. FROM t1
  42. INNER JOIN (SELECT t1_id, COUNT(*) AS RC
  43. FROM t1_2
  44. GROUP BY t1_id) Tab1_2 ON t1.id=Tab1_2.t1_id
  45. WHERE Tab1_2.RC=2
  46. 从表 t1 中选择 id name,与(从 t1_2 表中选择 t1_id,按 t1_id 分组并计算数量作为 RC 的子查询)Tab1_2 进行内连接,其中 t1.id 等于 Tab1_2.t1_id,且 Tab1_2.RC 等于 2
  47. Output:
  48. 输出:
  49. +----+------+
  50. | id | name |
  51. +----+------+
  52. | 1 | aaa |
  53. +----+------+
  54. 结果:
  55. > ---- after your question update
  56. > ---- 在您的问题更新后
  57. example data
  58. 示例数据
  59. ...
  60. ...
  61. (中间部分省略)
  62. Query
  63. 查询
  64. SELECT t1.id,t1.name
  65. FROM t1
  66. INNER JOIN (SELECT DISTINCT t1_id
  67. FROM t1_2
  68. WHERE t1_2.t2_id IN (1,2)
  69. ) t12 ON t1.id=t12.t1_id
  70. 从表 t1 中选择 t1.id t1.name,与(从 t1_2 表中选择 DISTINCT t1_id,其中 t1_2.t2_id (1,2) 中)的子查询 t12 进行内连接,其中 t1.id 等于 t12.t1_id
  71. Output
  72. 输出
  73. +----+------+
  74. | id | name |
  75. +----+------+
  76. | 1 | aaa |
  77. | 3 | ccc |
  78. +----+------+
  79. 结果
英文:

Others already answered, I just propose a slight different way:
Example data:

  1. CREATE TABLE t1(
  2. id INT,
  3. name VARCHAR(30)
  4. );
  5. INSERT INTO t1 VALUES
  6. (1, 'aaa'),
  7. (2, 'bbb'),
  8. (3, 'ccc'),
  9. (4, 'ddd');
  10. CREATE TABLE t2(
  11. id INT,
  12. name VARCHAR(30)
  13. );
  14. INSERT INTO t2 VALUES
  15. (1, 'zzz'),
  16. (2, 'yyy'),
  17. (3, 'xxx'),
  18. (4, 'www');
  19. CREATE TABLE t1_2(
  20. t1_id INT,
  21. t2_id INT
  22. );
  23. INSERT INTO t1_2 VALUES
  24. (1, 1),
  25. (1, 2),
  26. (3, 3),
  27. (4, 4);

Query:

  1. SELECT id, name
  2. FROM t1
  3. INNER JOIN (SELECT t1_id, COUNT(*) AS RC
  4. FROM t1_2
  5. GROUP BY t1_id) Tab1_2 ON t1.id=Tab1_2.t1_id
  6. WHERE Tab1_2.RC=2

Output:

  1. +----+------+
  2. | id | name |
  3. +----+------+
  4. | 1 | aaa |
  5. +----+------+

> ---- after your question update

example data

  1. ...
  2. CREATE TABLE t1_2(
  3. t1_id INT,
  4. t2_id INT
  5. );
  6. INSERT INTO t1_2 VALUES
  7. (1, 1),
  8. (1, 2),
  9. (3 ,1),
  10. (3,2),
  11. (3, 3),
  12. (4, 4);

Query

  1. SELECT t1.id,t1.name
  2. FROM t1
  3. INNER JOIN (SELECT DISTINCT t1_id
  4. FROM t1_2
  5. WHERE t1_2.t2_id IN (1,2)
  6. ) t12 ON t1.id=t12.t1_id

Output:

  1. +----+------+
  2. | id | name |
  3. +----+------+
  4. | 1 | aaa |
  5. | 3 | ccc |
  6. +----+------+

huangapple
  • 本文由 发表于 2023年5月24日 19:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323159.html
匿名

发表评论

匿名网友

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

确定