sql内连接单表
Posted
技术标签:
【中文标题】sql内连接单表【英文标题】:sql inner join single table 【发布时间】:2016-01-25 19:35:20 【问题描述】:三列表格; ID、术语、同义词
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="IDENTITY" SYNONYM="CHARACTER" SYNONYM="EXISTENCE" SYNONYM="IDENTIFICATION" SYNONYM="INTEGRITY" SYNONYM="NAME"
2、TERM="CIRCUMSTANCES" SYNONYM="ASSETS" SYNONYM="CAPITAL" SYNONYM="CHANCES"
【问题讨论】:
请澄清您的问题。 ***.com/help/how-to-ask 【参考方案1】:如果你使用 mysql,可以使用GROUP_CONCAT
SELECT MIN(`ID`), `TERM`, GROUP_CONCAT(`SYNONYM`, ' | ')
FROM my_table
GROUP BY `TERM`;
【讨论】:
感谢您的帖子。GROUP_CONCAT 无法识别【参考方案2】:Oracle 解决方案 - 从 11g 开始:
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
【讨论】:
它与 DB2 相悖,不喜欢 WITHIN,但我现在有一张很好的图片,并且会解决它。谢谢!以上是关于sql内连接单表的主要内容,如果未能解决你的问题,请参考以下文章