如何从联接中的字段中选择/搜索?
最好将其保留原样或将查询分成两部分,因为where子句在连接表中搜索</ p>
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')
答案 0 :(得分:1)
如果要有效执行,请在where
子句中包含join
子句。
SELECT
*
FROM
user u
LEFT JOIN
country c ON c.id=u.country_id
WHERE
c.code='dk'
SELECT
*
FROM
user u
LEFT JOIN
country c ON c.id=u.country_id
AND c.code='dk'
此代码将仅生成1个db查询,您的所有示例都将生成2个查询。