http://sqlfiddle.com/#!9/429f91/10
我有一个源表,例如,我从中收集了同义词对到临时表中。
+-------+---------+--------+-----------+
| Name | Synonym | NameID | SynonymID |
+-------+---------+--------+-----------+
| Yes | Oui | 1 | 2 |
| Yes | Da | 1 | 3 |
| Yes | Si | 1 | 4 |
| Oui | Yes | 2 | 1 |
| Oui | Da | 2 | 3 |
| Oui | Si | 2 | 4 |
| Da | Yes | 3 | 1 |
| Da | Oui | 3 | 2 |
| Da | Si | 3 | 4 |
| Si | Yes | 4 | 1 |
| Si | Da | 4 | 3 |
| Si | Oui | 4 | 2 |
| Red | Rouge | 5 | 6 |
| Rouge | Red | 6 | 5 |
+-------+---------+--------+-----------+
的目标是从源表中删除除单个词以外的所有词,因此它没有同义词。上面的SqlFiddle包含表并选择,但我不清楚如何
对同义词进行分组(例如,在本例中为“是”,“ Oui”,“ Da”,“ Si”和“红色”,“胭脂”
在所有组中都选择Min(ID)
本质上希望做类似的事情:
Delete from SourceTable where ID in (SELECT QUERY FROM TEMP TABLE WITH ALL BUT MIN IDS FROM EACH GROUP
在这种情况下会重新出现
Delete from SourceTable where ID in (2,3,4,6)
答案 0 :(得分:0)
您的条件是:“不应该存在ID较小的同义词”-可以将其写为NOT EXISTS子查询:
select distinct NameID
from Synonyms s
where not exists (
select *
from Synonyms s1
where s1.SynonymID = s.NameID
and s1.NameID < s.NameID
)
这将返回ID 1和5。
另一种方法是
select distinct s.NameID
from Synonyms s
left join Synonyms s1
on s1.SynonymID = s.NameID
and s1.NameID < s.NameID
where s1.SynonymID is null
获取相反的结果集更加简单:
select distinct SynonymID
from Synonyms
where SynonymID > NameID
这将返回2,3,4,6