一个 Item 有许多 ItemDetails。ItemDetail 具有“type”、“value”和“item_id”字段。
当且仅当项目具有受某些可变条件限制的确切ItemDetails 时,我才需要找到所有项目。例如,我需要查找 ItemDetails 为 (type=10, value=1000) 和 (type=20 and value=2000) 的所有项目
我的第一个解决方案是这样的:
select p.*
from item p
where not exists
(
select c.id from item_detail c
where c.item_id=p.id
and (c.type<>10 or c.value<>1000)
and (c.type<>20 or c.value<>2000)
);
-- Execution Time: 17.819 ms
Run Code Online (Sandbox Code Playgroud)
但我意识到它只获取一个 ItemDetail(type=10, value=1000) 的项目。然后我发现了这个问题并改变了如下查询。
select p.*
from item p
where not exists
(
select c.id from item_detail c
where c.item_id=p.id
and (c.type<>10 or c.value<>1000)
and (c.type<>20 or c.value<>2000) …Run Code Online (Sandbox Code Playgroud)