你是什麼意思「用戶的陣列?」你的意思是存儲一個集合數據類型?一個CSV列表?術語「數組」並不真正適合Oracle領域。
此外,根據您要求的關鍵是電子郵件的性質,而不是其中一個ID,否則您會獲得每個組合。
例如,用戶a和b每個都有電子郵件「[email protected]」。您的查詢將有
一個,[email protected],B B,[email protected],一個
我想你想要的是
[email protected],(一,b)
現在,爲了使該領域ID的CSV列表,你可以使用:
如果你想的ID存儲在Oracle集合中,我會引導你朝這個方向了。
編輯:根據您的評論。
OK,如果你想要的全部結果,然後ammend到
SELECT a.id as id
a.email as email,
listagg(b.id,',') WITHIN GROUP (ORDER BY ID) as list_of_ids
FROM users a, users b
where a.email = b.email
and a.id != b.id
GROUP BY a.id, a.email
因此,如果用戶A,B和C都共享電子郵件[email protected]您將獲得:
一個, [email protected],「b,c」
b,bob @ inter。網, 「A,C」
C,[email protected], 「A,B」
如果你想從查詢刪除的電子郵件,然後:順便說一句
SELECT a.id as id
listagg(b.id,',') WITHIN GROUP (ORDER BY ID) as list_of_ids
FROM users a, users b
where a.email = b.email
and a.id != b.id
GROUP BY a.id
,如果您使用的是不支持listagg字符串聚合函數的Oracle舊版本,那麼您將能夠在此處找到備用解決方案:http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php
我建議使用適用於Oracle 11.1或10的wm_concat()等效,或者Oracle 9的其他人之一。
因此,對Oracle 11.1或10,使用:
SELECT a.id as id
a.email as email,
wm_concat(b.id) as list_of_ids
FROM users a, users b
where a.email = b.email
and a.id != b.id
GROUP BY a.id, a.email
你在使用。 mssql,mysql,oracle等? – Arion 2012-03-28 13:51:23
oracle sql developer – Ofer 2012-03-28 13:52:26