Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 3 years ago.
I have three tables:
- Table 1 contains a primary key.
- Table 2 contains a foreign key that is equal to primary key of table 1.
- Table 3 contains a foreign key that is equal to primary key of table 2.
QUESTION: Is it possible to SELECT information from table 2 and 3 while only knowing the primary key of table 1? If so please give a brief example of the SELECT QUERY. I have tried doing an Inner join but got a syntax error.
Yes you can.
Example:
table_a table_b table_c
_______________ _______________ _______________
| id | name | | id | gender | | id | age |
|------+--------| |------+--------| |------+--------|
| 1 | sam | | 1 | m | | 1 | 18 |
|------+--------| |------+--------| |------+--------|
| 2 | ana | | 2 | f | | 2 | 22 |
|------+--------| |------+--------| |------+--------|
In order to get the following result:
_________________________________
| id | name | gender | age |
|------+--------+--------+--------|
| 1 | sam | m | 18 |
|------+--------+--------+--------|
| 2 | ana | f | 22 |
You could use the following SQL statement:
SELECT a.id, a.name, b.gender, c.age
FROM table_a AS a
LEFT JOIN table_b AS b
ON a.id = b.id
LEFT JOIN table_c AS c
ON a.id = c.id
P.S.: only answered this to do the ascii art xD!