SQL Inner join more than two tables

后端 未结 10 1083
感情败类
感情败类 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:12

    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

    0 讨论(0)
  • 2020-11-30 00:12

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 00:23

    Please find inner join for more than 2 table here

    Here are 4 table name like

    1. Orders
    2. Customers
    3. Student
    4. Lecturer

    So the SQL code would be:

    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;
    
    0 讨论(0)
提交回复
热议问题