Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

MySQL JOIN Multiple Joins di tabel yang sama?

Anda memerlukan 2 gabungan terpisah untuk setiap negara/kota/wilayah. di bawah ini adalah sintaks dasar, Anda mungkin perlu mengubahnya sedikit karena saya belum memasukkannya ke dalam parser:

SELECT people.first_name AS "First Name", people.last_name AS "Last Name", 
countries1.name AS "Country1", territories1.name AS "Territory1", cities1.name AS "City1", 
countries2.name AS "Country2", territories2.name AS "Territory2", cities2.name AS "City2"
FROM adb_people AS people
JOIN root_cities AS cities1 ON people.city1 = cities1.id
  AND people.city2 = cities1.id
JOIN root_territories AS territories1 ON people.prov_state1 = territories1.id
  AND people.prov_state2 = territories1.id
JOIN root_countries AS countries1 ON people.country1 = countries1.id
JOIN root_cities AS cities2 ON people.city2 = cities2.id
  AND people.city2 = cities2.id
JOIN root_territories AS territories2 ON people.prov_state2 = territories2.id
  AND people.prov_state2 = territories2.id
JOIN root_countries AS countries2 ON people.country2 = countries2.id


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. mysql - perlu dua batasan?

  2. PILIH dari database mysql pada panggilan javascript

  3. Penggunaan PDO di kelas

  4. Bagaimana cara mengatur ASP.NET MVC 2 dengan MySQL?

  5. Fitur pencarian dengan beberapa kriteria - PHP/MySQL