2016-01-25 86 views
-6

三列表; ID,TERM,同義詞sql內連接單表

1, IDENTITY, CHARACTER 
2, IDENTITY, EXISTENCE 
3, IDENTITY, IDENTIFICATION 
4, IDENTITY, INTEGRITY 
5, IDENTITY, NAME 
6, CIRCUMSTANCES, ASSETS 
7, CIRCUMSTANCES, CAPITAL 
8, CIRCUMSTANCES, CHANCES 

我需要的結果返回不同的「術語」有關聯「同義字(詞)」消除多對多並帶回一對多作爲結果。

1, IDENTITY, CHARACTER | EXISTENCE | IDENTIFICATION | INTEGRITY | NAME 
6, CIRCUMSTANCES, ASSETS |CAPITAL | CHANCES 

或者,

1,TERM = 「同一性」 SYNONYM = 「字符」 SYNONYM = 「存在」 SYNONYM = 「識別」 SYNONYM = 「完整性」 SYNONYM = 「NAME」

2 ,TERM = 「情況」 SYNONYM = 「資產」 SYNONYM = 「CAPITAL」 SYNONYM = 「CHANCES」

+2

請澄清你的問題。 – dan08

+2

http://stackoverflow.com/help/how-to-ask – Sebas

+3

哪個rdbms好嗎? – muratgu

回答

1

如果你正在使用MySQL,你可以使用GROUP_CONCAT

SELECT MIN(`ID`), `TERM`, GROUP_CONCAT(`SYNONYM`, ' | ') 
FROM my_table 
GROUP BY `TERM`; 
+0

感謝post.GROUP_CONCAT未被識別 – keydrive

0

Oracle解決方案 - 因爲11克:

with sample as 
(
    select 1 ID, 'IDENTITY' term , 'CHARACTER' syn from dual 
    union all 
    select 2, 'IDENTITY', 'EXISTENCE' from dual 
    union all 
    select 3, 'IDENTITY', 'IDENTIFICATION' from dual 
    union all 
    select 4, 'IDENTITY', 'INTEGRITY' from dual 
    union all 
    select 5, 'IDENTITY', 'NAME' from dual 
    union all 
    select 6, 'CIRCUMSTANCES', 'ASSETS' from dual 
    union all 
    select 7, 'CIRCUMSTANCES', 'CAPITAL' from dual 
    union all 
    select 8, 'CIRCUMSTANCES', 'CHANCES' from dual 
) 
select min(id) id, term, 
listagg(syn,'|') within group(order by syn) syns 
from sample 
group by term; 

輸出:

 ID TERM   SYNS 
---------- ------------- ------------------------------------------------------ 
     6 CIRCUMSTANCES ASSETS|CAPITAL|CHANCES 
     1 IDENTITY  CHARACTER|EXISTENCE|IDENTIFICATION|INTEGRITY|NAME 
+0

這是針對DB2的,不喜歡WITHIN,但我現在已經有了一個好的圖片,並且會通過它。謝謝! – keydrive