SQL 内连接两个以上的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14995000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:47:57  来源:igfitidea点击:

SQL Inner join more than two tables

sqlinner-join

提问by Ben Pearce

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");

回答by Alex

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

回答by Harjeet Jadeja

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 :

下面是连接三个或更多表的通用 SQL 查询语法。此 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

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.

我们首先连接表 1 和表 2,它们生成一个临时表,其中包含来自 table1 和 table2 的组合数据,然后将其连接到 table3。这个公式可以将3个以上的表扩展到N个表,你只需要确保SQL查询应该有N-1个连接语句才能连接N个表。就像连接两个表,我们需要 1 个连接语句,连接 3 个表,我们需要 2 个连接语句。

回答by vikramrajc

A possible solution:

一个可能的解决方案:

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='60000';

-- tets changes

-- tets 变化

回答by ravindra kalambe

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;

回答by subramanya46

select WucsName as WUCS_Name,Year,Tot_Households,Tot_Households,Tot_Male_Farmers  from tbl_Gender
INNER JOIN tblWUCS
ON tbl_Gender.WUCS_id =tblWUCS .WucsId 
INNER JOIN tblYear
ON tbl_Gender.YearID=tblYear.yearID

There are 3 Tables 1. tbl_Gender 2. tblWUCS 3. tblYear

有 3 个表 1. tbl_Gender 2. tblWUCS 3. tblYear

回答by shivaji Lohar

Please find inner join for more than 2 table here

请在此处找到 2 个以上表的内连接

Here are 4 table name like

这里有 4 个表名,例如

  1. Orders
  2. Customers
  3. Student
  4. Lecturer
  1. 订单
  2. 顾客
  3. 学生
  4. 讲师

So the SQL code would be:

所以 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;

回答by Chibuzor

try this method given below, modify to suit your need.

试试下面给出的这个方法,修改以满足您的需要。

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'

回答by Prio

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;

回答by CloudyMarble

The right syntaxis like:

正确的语法是这样的:

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

Orthe last line joining table3 on table1 like:

或者在 table1 上加入 table3 的最后一行,例如:

ON table3.ForeignKey= table1.PrimaryKey

回答by priya uthaya

Try this Here the syntax is

试试这个 这里的语法是

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

for example: 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

例如: 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