3 回答
TA贡献1839条经验 获得超15个赞
该where子句过滤掉行left join不成功的行。将其移至联接:
SELECT `settings`.*, `character_settings`.`value`
FROM `settings`
LEFT JOIN
`character_settings`
ON `character_settings`.`setting_id` = `settings`.`id`
AND `character_settings`.`character_id` = '1'
TA贡献1850条经验 获得超11个赞
在制作OUTER JOIN(ANSI-89或ANSI-92)时,过滤位置很重要,因为在创建JOIN之前ON应用了子句中指定的条件。进行JOIN后,将应用针对子句中提供的OUTER JOINed表的条件。这会产生非常不同的结果集。相比之下,如果or 子句中提供了条件,则对于INNER JOINs而言无关紧要-结果将是相同的。WHEREONWHERE
SELECT s.*,
cs.`value`
FROM SETTINGS s
LEFT JOIN CHARACTER_SETTINGS cs ON cs.setting_id = s.id
AND cs.character_id = 1
TA贡献1854条经验 获得超8个赞
如果我正确理解了您的问题,那么您需要设置数据库中的记录,如果它们没有跨character_settings表的联接,或者该联接记录的character_id = 1。
因此,您应该这样做
SELECT `settings`.*, `character_settings`.`value`
FROM (`settings`)
LEFT OUTER JOIN `character_settings`
ON `character_settings`.`setting_id` = `settings`.`id`
WHERE `character_settings`.`character_id` = '1' OR
`character_settings`.character_id is NULL
- 3 回答
- 0 关注
- 487 浏览
添加回答
举报