SQL Inner join more than two tables

后端 未结 10 1170
感情败类
感情败类 2020-11-29 23:21

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` 
         


        
10条回答
  •  忘掉有多难
    2020-11-30 00:18

    Here is a general SQL query syntax to join three or more table. This SQL query should work in all major relation database e.g. MySQL, Oracle, Microsoft SQLServer, Sybase and PostgreSQL :

    SELECT t1.col, t3.col FROM table1 join table2 ON table1.primarykey = table2.foreignkey
                                      join table3 ON table2.primarykey = table3.foreignkey
    

    We first join table 1 and table 2 which produce a temporary table with combined data from table1 and table2, which is then joined to table3. This formula can be extended for more than 3 tables to N tables, You just need to make sure that SQL query should have N-1 join statement in order to join N tables. like for joining two tables we require 1 join statement and for joining 3 tables we need 2 join statement.

提交回复
热议问题