Home > Back-end >  SELECT parent Column data from child row
SELECT parent Column data from child row

Time:10-04

I have 3 tables, table 1 inherits data from tables 2 and 3. I would like to fetch the parent tables data associated with its correlating row in table 1.

 -------------      ---------      --------- 
|   TABLE 1   |    | TABLE 2 |    | TABLE 3 |
 --- ---- ----      ---- ----      ---- ---- 
|row|chd1|chd2|    |pnt1|dat1|    |pnt2|dat2|
 --- ---- ----      ---- ----      ---- ---- 
| 1 |  A | Z  |    | A  | 55 |    | Z  | UI |
| 2 |  B | Y  |    | B  | 62 |    | X  | AD |
| 3 |  B | Z  |    | C  | 90 |    | Y  | MI |
| 4 |  D | U  |    | D  | 10 |    | U  | PA |
 --- ---- ----      ---- ----      ---- ---- 

Assuming I am looking for a result from row 2 in TABLE 1, the result I am looking for would be..

 ----- ------ ------ ------ ------ 
| row | chd1 | dat1 | chd2 | dat2 |
 ----- ------ ------ ------ ------ 
|  1  |   B  |  62  |   Y  |  MI  |
 ----- ------ ------ ------ ------ 

CodePudding user response:

Solution provided by OP, but originally edited into question.

Thanks to Dai, Inner join was the solution. Below is sample code that worked for me.

SELECT * FROM table_1
 INNER JOIN table_2 ON table_1.chd1 = table_2.pnt1
  INNER JOIN table_3 ON table_1.chd2 = table_3.pnt2 WHERE row = 2

CodePudding user response:

Does this work for you:

SELECT row, chd1, dat1, chd2, dat2 FROM 
table 1, table 2, table 3 
WHERE chd1 = pnt1 AND ch2=dat2
  • Related