Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Inner join more than two tables

Tags:

sql

inner-join

I can currently query the join of two tables on the equality of a foreign/primary key in the following way.

 $result = mysql_query("SELECT * FROM `table1`                           INNER JOIN                         `table2` ON table1.primaryKey=table2.table1Id"); 

I'd like to extend this to multiple tables (all with the same foreign keys). I am trying the following code which is not returning anything. Can anyone point out what I'm doing wrong?

 $result = mysql_query("SELECT * FROM `table1`                          INNER JOIN `table2`                          INNER JOIN table3                          ON table1.primaryKey=table2.table1Id=table3.table1Id"); 
like image 280
Ben Pearce Avatar asked Feb 21 '13 05:02

Ben Pearce


People also ask

Can you inner join more than 2 tables SQL?

In SQL Server, you can join more than two tables in either of two ways: by using a nested JOIN , or by using a WHERE clause. Joins are always done pair-wise.

Can inner join be for 3 tables?

The most common way of joining three tables goes something like this: SELECT * FROM Table1 INNER JOIN Table2 ON Condition INNER JOIN Table3 ON Condition; This uses an inner join, but you can specify your desired join type as with any other join. You can also combine join types if required (example below).


1 Answers

SELECT *  FROM table1  INNER JOIN table2       ON table1.primaryKey=table2.table1Id INNER JOIN table3       ON table1.primaryKey=table3.table1Id 
like image 175
Alex Avatar answered Sep 20 '22 15:09

Alex