你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所以我將不得不按陣列順序排列我的數組根據查詢? –