小编典典

Left Join With Where 子句

all

我需要从设置表中检索所有默认设置,但如果存在 x 字符,还需要获取字符设置。

但是此查询仅检索字符 = 1 的那些设置,而不是用户尚未设置任何人的默认设置。

SELECT `settings`.*, `character_settings`.`value`
FROM (`settings`)
LEFT JOIN `character_settings` 
ON `character_settings`.`setting_id` = `settings`.`id`
WHERE `character_settings`.`character_id` = '1'

所以我应该需要这样的东西:

array(
    '0' => array('somekey' => 'keyname', 'value' => 'thevalue'),
    '1' => array('somekey2' => 'keyname2'),
    '2' => array('somekey3' => 'keyname3')
)

其中 key 1 和 2 是 key 0 包含默认值和字符值时的默认值。


阅读 80

收藏
2022-06-28

共1个答案

小编典典

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'
2022-06-28