2013-08-12 76 views
0

我一直無法弄清楚如何使這個查詢工作。MySQL查詢和聯接

我有一個人和他們的個人數據表。 我有表讓我們把它安置

假設該表有以下字段:

PEOPLE 
id, code, name, lastname 

HOUSES 
id, codeowner, codeintermediate, codebuyer, area, numberofrooms 

如果我有三列,與同桌的關係(人)我怎麼能讓一個LEFT JOIN工作?

如果業主,中間體和購房人分開後,我會使用類似

"SELECT 
houses.*, 

owners.name AS ownersname, 
intermediates.name AS intermediatesname, 
buyers.lname AS buyersname 
FROM houses 
LEFT JOIN owners ON houses.codeowner = owners.code 
LEFT JOIN intermediates ON houses.codeintermediate = intermediates.code 
    LEFT JOIN buyers ON houses.codebuyer = buyers.code 

但我怎麼可以讓一個單身的人表這項工作?我怎樣才能使用別名等?事先謝謝!

回答

1

加入百姓餐桌的3倍

SELECT 
houses.*, 

owners.name AS ownersname, 
intermediates.name AS intermediatesname, 
buyers.name AS buyersname 
FROM houses 
LEFT JOIN people as owners ON houses.codeowner = owners.code 
LEFT JOIN people as intermediates ON houses.codeintermediate = intermediates.code 
LEFT JOIN people as buyers ON houses.codebuyer = buyers.code 
+0

它非常完美,我只是不知道你能做到這一點。謝謝! – JuanBonnett