你WHERE
子句选择行,其中empgroupid
是500或501,而不是empid
S其中所有empgroupid
S型阵列[500, 501]
。
您可以在HAVING
子句中使用ARRAY_AGG
:
SELECT empid
FROM empgroupinfo
GROUP BY empid
-- ORDER BY clause here is important, as array equality checks elements position by position, not just 'same elements as'
HAVING ARRAY_AGG(DISTINCT empgroupid ORDER BY empgroupid) = ARRAY[500, 501]
具体情况取决于[500, 501]
阵列从何而来,你可能不知道自己是否是排序或不。在这种情况下,“包含AND包含于”(运营商@>
和<@
)也应该起作用。
#= CREATE TABLE empgroupinfo (empid int, empgroupid int);
CREATE TABLE
Time: 10,765 ms
#= INSERT INTO empgroupinfo VALUES (1, 500), (1, 501), (2, 500), (2, 501), (2, 502);
INSERT 0 5
Time: 1,451 ms
#= SELECT empid
FROM empgroupinfo
GROUP BY empid
HAVING ARRAY_AGG(empgroupid ORDER BY empgroupid) = ARRAY[500, 501];
┌───────┐
│ empid │
├───────┤
│ 1 │
└───────┘
(1 row)
Time: 0,468 ms
@BunkerBoy:恐怕我不明白你的编辑。如果你不想'empId = 102',只需在查询中添加一个'WHERE empId <> 102'。 – Marth
对不起,我犯了一个错误,它不是关于empid!= 102谢谢我现在纠正了谢谢,它为我工作.. –
告诉我一件事假设,如果我的groupids是400和345所以我将不得不按阵列顺序排列我的数组根据查询? –