如何从连接中的字段中选择/搜索?
是保持原样还是将查询分成两部分更好,因为 where 子句在连接表中搜索
一问
SELECT *
FROM user u
LEFT JOIN country c ON c.id=u.country_id
WHERE c.code='dk'
两个查询
SELECT id
FROM country
WHERE code='dk'
SELECT *
FROM user u
LEFT JOIN country c ON c.id=u.country_id
WHERE c.id='result from first query'
子查询
SELECT *
FROM user u
LEFT JOIN country c ON c.id=u.country_id
WHERE c.id IN (SELECT id FROM country WHERE code='dk')