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 |
+-----+------+------+------+------+
My Answer:
To achieve this, you can use a SQL query with JOIN operations to fetch the parent table data associated with the child row in TABLE 1. Here's an example query:
sql
SELECT t1.row, t1.chd1, t2.dat1, t1.chd2, t3.dat2
FROM TABLE1 t1
JOIN TABLE2 t2 ON t1.chd1 = t2.pnt1
JOIN TABLE3 t3 ON t1.chd2 = t3.pnt2
WHERE t1.row = 2;
This query will fetch the data from TABLE 1, TABLE 2, and TABLE 3 where the child values in TABLE 1 match the parent values in TABLE 2 and TABLE 3. The WHERE clause specifies that you are looking for the result from row 2 in TABLE 1.
You can adjust the query as needed based on your specific table and column names.
Rate this post
3 of 5 based on 7901 votesComments