mpsbhat
mpsbhat

Reputation: 2773

mysql foreign key foriegn key relationship

Say, myself having three MySQL tables as follows.

  1. Table 1 contains table1id, value columns.
  2. Table 2 contains table2id, value, table1id (as FK) columns.
  3. Table 3 contains table3id, value, table1id (as FK) columns.

Then is the following relationship valid?

select * from table1 t1 inner join table2 t2 on t1.table1id = t2.table1id

Upvotes: 1

Views: 59

Answers (3)

sagivmal
sagivmal

Reputation: 86

You can also write it this way:

SELECT * FROM table1 t1, table2 t2, table3 t3
    WHERE (t1.table1id=t2.table2id) AND (t1.table1id=t3.table3id); 

** If you want to join only the first 2 tables--Use the code until the AND

*** If you want to join all the tables-- Use the entire code.

Upvotes: 0

Piyush Gupta
Piyush Gupta

Reputation: 2179

Yes, It is possible.

This one is Joining for teable1 and table2

select * from test1 t1 inner join test2 t2 on t1.id = t2.id;

This one is joining all three tables,

SELECT * FROM test1 t1 
     INNER JOIN test2 t2 ON t1.id = t2.id 
     INNER JOIN test3 t3 ON t1.id = t3.id; 

Output: ONLINE DEMO HERE

Upvotes: 1

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3138

Try this

SELECT * FROM 
table1 t1 INNER JOIN table2 t2 
ON t1.table1id = t2.table2id 
INNER JOIN table3 t3
ON t1.table1id = t3.table3id 

Upvotes: 0

Related Questions