您好,我正在使用以下 SELECT
"SELECT
P.id
,P.price
,P.contract
,P.property_type
,CT.id
,CT.english_text
,PT.id
,PT.english
,C.cityname
,S.sectorname
,S.id
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 WHERE 1"
在最终结果中我总是有 id=2
,我认为它是来自 S.id
的 id,因为它是最后一个。
如何进入此查询的 foreach 循环以在 $row['id']
中显示 P.id
?
现在每个结果总是 $row['2']
。
最佳答案
您有多个名称相同的列,这会混淆 php。使用别名给它们不同的名字:
SELECT P.id as p_id, P.price, P.contract, P.property_type,
CT.id as c_id, CT.english_text,
PT.id as pt_id, PT.english
C.cityname,
S.sectorname, S.id as s_id
. . .
这会从结果集中删除所有 id
。你想要的是 s_id
。
关于php - 定义哪个 ID 应该保留在 JOIN 表查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910802/