2011-08-02 72 views
7
SELECT people.first_name AS "First Name", people.last_name AS "Last Name", countries.name AS "Country1", territories.name AS "Territory1", cities.name AS "City1", countries.name AS "Country2", territories.name AS "Territory2", cities.name AS "City2" 
FROM adb_people AS people 
JOIN root_cities AS cities ON people.city1 = cities.id 
AND people.city2 = cities.id 
JOIN root_territories AS territories ON people.prov_state1 = territories.id 
AND people.prov_state2 = territories.id 
JOIN root_countries AS countries ON people.country1 = countries.id 

Điều tôi đang cố gắng làm ở đây là liên kết Quốc gia1 (id) với Quốc gia1 (tên) và chỉ hiển thị tên. Ví dụ về mã này chỉ hoạt động nếu Quốc gia1, Lãnh thổ1, Thành phố1 giống với Quốc gia2, Lãnh thổ2, Thành phố2MySQL THAM GIA Nhiều Tham gia trên cùng một bảng?

Tôi sẽ hình ảnh vấn đề của mình là cách tôi đang thực hiện JOIN của mình. Tôi mới ở phía SQL của sự vật. Tôi đã đọc trên JOINS trên internet (tìm kiếm google và đọc vài hướng dẫn đầu tiên) tuy nhiên không có gì tôi đã đọc đã được bất kỳ trợ giúp trong trường hợp này.

Tôi thực sự đánh giá cao bất kỳ trợ giúp nào về những gì tôi đang làm sai ở đây. Có lẽ một cú huých đi đúng hướng?

Trả lời

14

bạn cần 2 tham gia riêng biệt cho mỗi quốc gia/thành phố/lãnh thổ. dưới đây là cú pháp cơ bản, bạn có thể cần phải thay đổi nó một chút như tôi đã không đặt nó thông qua một phân tích cú pháp:

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 
+0

Duh, Tại sao tôi không thử điều đó để bắt đầu. Tôi sẽ thực hiện nó ngay bây giờ và cho bạn biết làm thế nào nó đi. Cảm ơn bạn! – rlemon

+0

Câu trả lời này vẫn không có vấn đề rằng nó chỉ hoạt động nếu city1 = city2? –

+0

Không nên, không. Tôi đã chỉ chỉnh sửa các truy vấn bởi vì tôi quên thay đổi bí danh bảng của tôi trong phần lựa chọn từ bản sao/dán, nếu đó là những gì bạn đang đề cập đến? –

4
SELECT 
    people.first_name AS "First Name", 
    people.last_name AS "Last Name", 
    countries.name AS "Country1", 
    territories.name AS "Territory1", 
    cities.name AS "City1", 
    countries2.name AS "Country2", 
    territories2.name AS "Territory2", 
    cities2.name AS "City2" 
FROM 
    adb_people AS people 
    JOIN root_cities AS cities ON people.city1 = cities.id 
    jOIN root_cities AS cities2 people.city2 = cities2.id 
    JOIN root_territories AS territories ON people.prov_state1 = territories.id 
    JOIN root_territories AS territories2 ON people.prov_state2 = territories2.id 
    JOIN root_countries AS countries ON people.country1 = countries.id 
    JOIN root_countries AS countries2 ON people.country2 = countries2.id 
6

này sẽ làm các trick.

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