给定下表:
id category_id categorizable_id categorizable_type
66 22 67 Image
72 22 75 Image
74 15 71 Image
104 15 67 Image
126 15 113 Image
现在我想通过左连接选择那些“图像”,他们的 category_ids 是 22和15 我的 sql 目前看起来像这样:
SELECT images.id FROM images
LEFT OUTER JOIN category_links
ON images.id = category_links.categorizable_id AND category_links.categorizable_type = 'Image'
where
category_links.category_id = "22" AND
(
select count(*) from category_links
where category_links.categorizable_id = images.id AND
category_links.categorizable_type = 'Image' AND
category_links.category_id = "15"
)
如何重写以消除对子查询的需要?