SQL内部连接两个以上的表

我现在可以通过以下方式查询外键/主键相等的两个表的连接。

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

我想扩展到多个表(所有的外键相同)。 我正在尝试下面的代码不返回任何东西。 任何人都可以指出我做错了什么?

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

这是一个通用的SQL查询语法来join三个或更多的表。 这个SQL查询应该可以在所有主要的关系数据库中工作,例如MySQL,Oracle,Microsoft SQLServer,Sybase和PostgreSQL:

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

我们首先连接表1和表2,它们产生一个来自表1和表2的组合数据的临时表,然后将它们连接到表3。 这个公式可以扩展到超过3个表到N个表,你只需要确保SQL查询应该有N-1个连接语句才能连接N个表。 像join两个表一样,我们需要1个连接语句,而连接3个表我们需要2个连接语句。

可能的解决scheme:

 select Company.Company_Id,Company.Company_Name, Invoice_Details.Invoice_No, Product_Details.Price from Company inner join Invoice_Details on Company.Company_Id=Invoice_Details.Company_Id inner join Product_Details on Invoice_Details.Invoice_No= Product_Details.Invoice_No where Price='70000'; 

正确的语法如下所示:

 SELECT * FROM table1 INNER JOIN table2 ON table1.primaryKey = table2.ForeignKey INNER JOIN table3 ON table3.primaryKey = table2.ForeignKey 

在table1上jointable3的最后一行,如:

 ON table3.ForeignKey= table1.PrimaryKey 
 select * from Employee inner join [Order] On Employee.Employee_id=[Order].Employee_id inner join Book On Book.Book_id=[Order].Book_id inner join Book_Author On Book_Author.Book_id=Book.Book_id inner join Author On Book_Author.Author_id=Author.Author_id; 

请在这里find超过2桌的内连接

这里有4个表名

  1. 命令
  2. 顾客
  3. 学生
  4. 讲师

所以SQL代码将是:

 select o.orderid, c.customername, l.lname, s.studadd, s.studmarks from orders o inner join customers c on o.customrid = c.customerid inner join lecturer l on o.customrid = l.id inner join student s on o.customrid=s.studmarks; 

尝试下面给出的这种方法,修改以适应您的需要。

 SELECT employment_status.staff_type, COUNT(monthly_pay_register.age), monthly_pay_register.BASIC_SALARY, monthly_pay_register.TOTAL_MONTHLY_ALLOWANCES, monthly_pay_register.MONTHLY_GROSS, monthly_pay_register.TOTAL_MONTHLY_DEDUCTIONS, monthly_pay_register.MONTHLY_PAY FROM (monthly_pay_register INNER JOIN deduction_logs ON monthly_pay_register.employee_info_employee_no = deduction_logs.employee_no) INNER JOIN employment_status ON deduction_logs.employee_no = employment_status.employee_no WHERE monthly_pay_register.`YEAR`=2017 and monthly_pay_register.`MONTH`='may' 
 SELECT eb.n_EmpId, em.s_EmpName, deg.s_DesignationName, dm.s_DeptName FROM tbl_EmployeeMaster em INNER JOIN tbl_DesignationMaster deg ON em.n_DesignationId=deg.n_DesignationId INNER JOIN tbl_DepartmentMaster dm ON dm.n_DeptId = em.n_DepartmentId INNER JOIN tbl_EmployeeBranch eb ON eb.n_BranchId = em.n_BranchId; 

试试这里这里的语法是

 SELECT table1 .columnName, table3 .columnName FROM table1 inner join table2 ON table1.primarykey = table2.foreignkey inner join table3 ON table2.primarykey = table3.foreignkey 

例如: Select SalesHeader.invoiceDate,ActualSales,DeptName,tblInvDepartment.DeptCode ,LocationCode from SalesDetail Inner Join SalesHeader on SalesDetail.InvoiceNo = SalesHeader.InvoiceNo inner join tblInvDepartment on tblInvDepartment.DeptCode = SalesDetail.DeptCode