简体   繁体   中英

Get duplicates from one table for records from another

I've got three tables:

Table1:

id | name         
-------------------
1  | ExampleName1  
2  | ExampleName2 
3  | ExampleName 
4  | Example2Name 
5  | ExampleNamee 

Table2:

id  | text          | idTable1
-------------------------------
20  | ExampleText1  | 1
21  | Ext1          | 1
22  | Text1         | 3
22  | pleText1      | 2

Table3:

id  | login      | idTable1
----------------------------
20  | EEEEEEEE   | 1
5   | WWWWWWWWw  | 2
6   | QQQQQQQQQQ | 1
10  | TTTTTTTT   | 1
11  | OOOOOOOOO  | 2

And I would like to get all records from Table2 but if in table3 there are several records for a given table1ID I would like the data from table2 will be duplicated

example: Result from table2 where idTable1 = 1:

id | text           | idTable1 | Table3Login
---------------------------------------------
20 | ExampleText1   | 1        | EEEEEEEE   
20 | ExampleText1   | 1        | QQQQQQQQQQ 
20 | ExampleText1   | 1        | TTTTTTTT   
21 | Ext1           | 1        | EEEEEEEE 
21 | Ext1           | 1        | QQQQQQQQQQ 
21 | Ext1           | 1        | TTTTTTTT  

What type join to use?

Make the JOIN of your tables as follows:

Query:

 SELECT T2.id, T2.text, T2.idTable1, T3.login AS Table3Login FROM Table1 AS T1 JOIN Table2 AS T2 ON T1.id = T2.idTable1 JOIN Table3 AS T3 ON T2.idTable1 = T3.idTable1 ORDER BY T2.text;

Results:

 id | text | idtable1 |  table3login -: |:----------- |  -------: |:---------- 20 |  ExampleText1 | 1 |  EEEEEEEE 20 |  ExampleText1 | 1 |  QQQQQQQQQQ 20 |  ExampleText1 | 1 |  TTTTTTTT 21 |  Ext1 | 1 |  EEEEEEEE 21 |  Ext1 | 1 |  QQQQQQQQQQ 21 |  Ext1 | 1 |  TTTTTTTT 22 |  pleText1 |  2 |  WWWWWWWWw 22 | pleText1 |  2 |  OOOOOOOOO

Check this dbfiddle working sample here :


 CREATE TABLE Table1 ( id INT NOT NULL, name VARCHAR(50) ); CREATE TABLE Table2 ( id INT NOT NULL, text VARCHAR(50), idTable1 INT ); CREATE TABLE Table3 ( id INT NOT NULL, login VARCHAR(50), idTable1 INT );
 ✓ ✓ ✓ 
 ---- INSERT Table1: INSERT INTO Table1 (id, name) SELECT 1, 'ExampleName1'; INSERT INTO Table1 (id, name) SELECT 2, 'ExampleName2'; INSERT INTO Table1 (id, name) SELECT 3, 'ExampleName'; INSERT INTO Table1 (id, name) SELECT 4, 'Example2Name'; INSERT INTO Table1 (id, name) SELECT 5, 'ExampleNamee'; ---- INSERT Table2: INSERT INTO Table2 (id, text, idTable1) SELECT 20, 'ExampleText1', 1; INSERT INTO Table2 (id, text, idTable1) SELECT 21, 'Ext1', 1; INSERT INTO Table2 (id, text, idTable1) SELECT 22, 'Text1', 3; INSERT INTO Table2 (id, text, idTable1) SELECT 22, 'pleText1', 2; ---- INSERT Table3: INSERT INTO Table3 (id, login, idTable1) SELECT 20, 'EEEEEEEE', 1; INSERT INTO Table3 (id, login, idTable1) SELECT 5, 'WWWWWWWWw', 2; INSERT INTO Table3 (id, login, idTable1) SELECT 6, 'QQQQQQQQQQ', 1; INSERT INTO Table3 (id, login, idTable1) SELECT 10, 'TTTTTTTT', 1; INSERT INTO Table3 (id, login, idTable1) SELECT 11, 'OOOOOOOOO', 2;
 SELECT * FROM Table1;
 id | name -: |:----------- 1 | ExampleName1 2 | ExampleName2 3 | ExampleName 4 | Example2Name 5 |  ExampleNamee
SELECT * FROM Table2;
 id | text | idtable1 -: |:----------- |  -------: 20 |  ExampleText1 | 1 21 |  Ext1 | 1 22 |  Text1 | 3 22 |  pleText1 |  2 
 SELECT * FROM Table3;
 id | login | idtable1 -: |:--------- |  -------: 20 |  EEEEEEEE |  1 5 |  WWWWWWWWw | 2 6 |  QQQQQQQQQQ |  1 10 |  TTTTTTTT |  1 11 |  OOOOOOOOO |2 

This is the query:

 -- This is the query: SELECT T2.id, T2.text, T2.idTable1, T3.login AS Table3Login FROM Table1 AS T1 JOIN Table2 AS T2 ON T1.id = T2.idTable1 JOIN Table3 AS T3 ON T2.idTable1 = T3.idTable1 ORDER BY T2.text;

Results:

 id | text | idtable1 |  table3login -: |:----------- |  -------: |:---------- 20 |  ExampleText1 | 1 |  EEEEEEEE 20 |  ExampleText1 | 1 |  QQQQQQQQQQ 20 |  ExampleText1 | 1 |  TTTTTTTT 21 |  Ext1 | 1 |  EEEEEEEE 21 |  Ext1 | 1 |  QQQQQQQQQQ 21 |  Ext1 | 1 |  TTTTTTTT 22 |  pleText1 |  2 |  WWWWWWWWw 22 | pleText1 |  2 |  OOOOOOOOO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM