您好我使用以下查询从表属性中选择数据,其中一些列从另一个表中获取id的实际数据。我想问一下是否有办法让这个查询使用从JOINed表中选择数据但是要使用WHERE子句中表属性的值?
$query= "SELECT
P.id
,P.price
,P.contract
,P.property_type
,P.sector
,P.title
,P.address
,P.bedrooms
,P.bathrooms
,P.price
,P.m2
,P.text_english
,P.photo_01
,P.utilities
,P.google_maps
,P.date
,CT.id
,CT.english_text
,PT.id
,PT.english
,C.cityname
,S.sectorname
,S.id
,O.ownername
,O.phone_one
,O.phone_two
,O.email
,O.notes
FROM properties P
JOIN contract CT
ON CT.id = P.contract
JOIN property_type PT
ON PT.id = P.property_type
JOIN city C
ON C.id = P.city
JOIN sector S
ON S.id = P.sector
JOIN owner O
ON O.id = P.owner FROM properties WHERE 1";
if (!empty($sector)) { $query .= " P.sector = '$sector'"; }
if (!empty($property_type)) { $query .= " AND P.property_type = '$property_type'"; }
if (!empty($contract)) { $query .= " AND P.contract = '$contract'"; }
if (!empty($minimum_price)) { $query .= " AND P.price BETWEEN '$minimum_price' AND '$maximum_price'"; }
if (!empty($m2_from)) { $query .= " AND P.m2 BETWEEN '$m2_from' AND '$m2_until'"; }
if (!empty($bedrooms)) { $query .= " AND P.bedrooms = '$bedrooms'"; }
如果我添加更多代码,如:
,CT.id
然后:if (!empty($contract)) { $query .= " AND CT.id = '$contract'"; }
因为合同中的id是属性内部列合约中的值,所以它也不起作用。