我无法弄清楚如何使这个查询有效。
我有一张人员及其个人资料表。 我有一张让我们称之为
的表格假设表中包含以下字段:
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
但是如何使用单个PEOPLE表进行此操作呢?我该如何使用别名呢?先谢谢你了!
答案 0 :(得分: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