这是我当前的查询:

SELECT `performers`.`hash`,
        `performers`.`alias`,
        `performers`.`date_updated`,
        `performers`.`status`,
        IF(`performers`.`status` = 'active', 'deleted','active') AS `statususe`,
        `images`.`image_hash_file`
FROM `performers`
LEFT JOIN `images` ON `images`.`asset_id` = `performers`.`id`
WHERE (`images`.`asset_type` = 'performer')
ORDER BY `alias` ASC LIMIT 12`


其中有一个where子句

WHERE (`images`.`asset_type` = 'performer')


我希望它是可选的,这样,如果where子句不合适,它仍会显示来自performers表的记录,这些记录没有与令人满意的图像记录的联接。

最佳答案

您可以在LEFT JOIN中添加WHERE子句,以便不匹配的行仍在结果集中。

SELECT `performers`.`hash`, `performers`.`alias`,
  `performers`.`date_updated`, `performers`.`status`,
  IF(`performers`.`status` = 'active', 'deleted','active') AS `statususe`,
  `images`.`image_hash_file`
FROM `performers`
LEFT JOIN `images` ON `images`.`asset_id` = `performers`.`id` AND `images`.`asset_type` = 'performer'
ORDER BY `alias` ASC
LIMIT 12


WHERE和ON子句之间的主要区别在于,当所有内容都加入后,WHERE子句起作用,使得不匹配的行从结果集中删除时,ON子句在JOIN期间起作用。

09-20 09:57