我很难构建三联接查询。
我有三张桌子:
内部
house ID(key) | Address | personID |
人:
personID (key) | Name | Address |
图片 // @ house
imageID (key) | personID | url |
我想将结果限制为 5 。
我想查询house address-es(main),它的所有者名称&地址,以及业主的一张照片。
注意:每个人最多有3张图像(图像表中有3行),但只需要一行,无论哪一个都无关紧要。
答案 0 :(得分:21)
SELECT h.Address, p.Name, p.Address as OwnerAddress, i.url FROM house AS h
INNER JOIN person AS p ON p.personID = h.personID
INNER JOIN images AS i ON i.personID = p.personID
GROUP BY h.houseID
应该适合你。
答案 1 :(得分:2)
也许你最好使用LEFT OUTER JOIN,比如
SELECT
h.Address as `h_address`,
p.Name as `p_name`,
p.Address as `p_address`,
i.url as `i_url`
FROM house AS `h`
LEFT OUTER JOIN person AS `p` ON (p.personID = h.personID)
LEFT OUTER JOIN images AS `i` ON (p.personID = i.personID)
GROUP BY h.houseID
它还会显示没有注册图像的房屋。
答案 2 :(得分:-1)
加入三张桌子
即:
select i_f.*, i_f_d.* , i_f_c.creator_name
from indent_form i_f
join indent_form_details i_f_d on i_f.id=i_f_d.ind_id
join indent_form_creator i_f_c on i_f.user_id=i_f_c.id
indent_form table 1,
indent_form_details table 2,
indent_form_creator table 3,