SQL INNER join więcej niż dwie tabele

Mogę obecnie odpytywać połączenie dwóch tabel o równości klucza obcego / podstawowego w następujący sposób.

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

Chciałbym rozszerzyć to na wiele tabel (wszystkie z tymi samymi kluczami obcymi). Próbuję poniższy kod, który nic nie zwraca. Czy ktoś może wskazać, co robię źle?

 $result = mysql_query("SELECT * FROM `table1` 
                        INNER JOIN `table2` 
                        INNER JOIN table3 
                        ON table1.primaryKey=table2.table1Id=table3.table1Id");
Author: Brian Tompsett - 汤莱恩, 2013-02-21

10 answers

SELECT * 
FROM table1 
INNER JOIN table2
      ON table1.primaryKey=table2.table1Id
INNER JOIN table3
      ON table1.primaryKey=table3.table1Id
 146
Author: Alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-08 15:42:52

Oto ogólna składnia zapytania SQL do połączenia trzech lub więcej tabeli. MySQL, Oracle, Microsoft SqlServer, Sybase i PostgreSQL:

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

Najpierw łączymy tabelę 1 i tabelę 2, które tworzą tymczasową tabelę z połączonymi danymi z table1 i table2, która następnie jest łączona z table3. Ta formuła może być rozszerzona dla więcej niż 3 tabel do N tabel, wystarczy upewnić się, że zapytanie SQL powinno mieć instrukcję N-1 join w aby dołączyć N tabel. podobnie jak w przypadku łączenia dwóch tabel, wymagamy 1 instrukcji join, a do łączenia 3 tabel potrzebujemy 2 instrukcji join.

 27
Author: Harjeet Jadeja,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-21 05:24:10
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;
 2
Author: ravindra kalambe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-04 13:27:21

Możliwe rozwiązanie:

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

-- zmiany tets

 1
Author: vikramrajc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-24 05:02:06

Składnia Prawa jest następująca:

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

Lub ostatni wiersz łączący table3 na table1 jak:

ON table3.ForeignKey= table1.PrimaryKey
 0
Author: CloudyMarble,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-21 05:15:22
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;
 0
Author: Prio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-18 02:29:15

Proszę znaleźć wewnętrzny join dla więcej niż 2 tabeli tutaj

Oto 4 nazwy tabeli jak

  1. zamówienia
  2. Klienci
  3. Student
  4. wykładowca

Więc kod SQL będzie:

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
Author: shivaji Lohar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-28 01:32:36

Wypróbuj tę metodę podaną poniżej, zmodyfikuj do własnych potrzeb.

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'
 0
Author: Chibuzor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-09 04:17:15
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

Są 3 tabele 1. tbl_Gender 2. tblWUCS 3. tblYear

 0
Author: subramanya46,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-21 12:04:56

Spróbuj tutaj składnia jest

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

Na przykład: 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

 -2
Author: priya uthaya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55