我有3张桌子(物业,设施,property_facility)
现在,我想单独从facility
表中获取所有设施以及属性数据到给定的属性ID。
我用LEFT JOIN
尝试了如下。但是我不能从设施表中获得所有设施。
SELECT property_id
, contract_id
, type_id
, location_id
, beds
, ROUND(price,3) as price
, f.facility_id
, f.name
, pf.facility_id
FROM facility f
LEFT JOIN property_facility pf ON pf.facility_id = f.facility_id AND pf.property_id = 6
LEFT JOIN property p USING(property_id)
WHERE p.property_id = 6
有人可以告诉我如何正确进行此查询吗?
最佳答案
您的WHERE
子句正在筛选出不匹配的功能。您需要将USING
切换为ON
并执行以下操作:
FROM facility f LEFT JOIN
property_facility pf
ON pf.facility_id = f.facility_id AND
pf.property_id = 6 LEFT JOIN
property p
ON p.property_id = pf.property_id AND p.property_id = 6
您似乎理解了这个概念,因为第一个
JOIN
使用相同的条件。好吧,实际上
property_id
不会改变。因此,您可以删除WHERE
子句,然后根据需要继续使用USING
。关于mysql - 选择查询与MySql左联接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50909732/