记录表:
ID, VID (Villa ID), NAME, PRICE
别墅表:
ID, NAME (Villa NAME), PHOTOS etc.
我使用这个SQL:
SELECT records.id, villa_name AS (SELECT name FROM villas WHERE id = records.vid), records.name
FROM records WHERE records.id = 5
有什么问题?
答案 0 :(得分:2)
尝试
SELECT records.id, records.name AS record_name, villas.name AS villa_name FROM records INNER JOIN villas ON records.vid = villas.id WHERE records.id = 5
编辑:Mark Bannister的建议,见下文。
答案 1 :(得分:1)
你应该在这里使用JOIN。
SELECT r.id, r.name, v.name
FROM records r
INNER JOIN villas v ON v.id = r.vid
WHERE records.id = 5;