[英]finding distinct pairs in sql
I was trying to learn non-equi joins when I encountered this problem.当我遇到这个问题时,我正在尝试学习非 equi 连接。 I have a table pops:
我有一张桌子弹出:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| country | varchar(100) | YES | | NULL | |
| continent | varchar(100) | YES | | NULL | |
| population | bigint(20) | YES | | NULL | |
I was trying to find countries with their population in the vicinity of say, 100.我试图找到人口在 100 附近的国家。
select distinct
p1.country,
p2.country,
p1.population,
p2.population
from pops p1
inner join pops p2
on p1.population between p2.population - 100 and p2.population + 100
and p1.country <> p2.country
where p2.country <> p1.country
output I got was:我得到的输出是:
+------------+------------+------------+------------+
| country | country | population | population |
+------------+------------+------------+------------+
| pakistan | india | 99988 | 99999 |
| china | india | 99990 | 99999 |
| bangladesh | japan | 999 | 999 |
| india | pakistan | 99999 | 99988 |
| china | pakistan | 99990 | 99988 |
| japan | bangladesh | 999 | 999 |
| india | china | 99999 | 99990 |
| pakistan | china | 99988 | 99990 |
+------------+------------+------------+------------+
as we can see, I am getting pairs of (india, pakistan) as well as (pakistan, india), which is data-wise the same thing.正如我们所见,我得到了成对的(印度、巴基斯坦)和(巴基斯坦、印度),这在数据方面是一样的。 Is it possible to eliminate one of the records from the pair?
是否有可能从一对记录中删除一个记录?
You could decide to always have the lexographically first (or last, for argument's sake) country on the p1 side - use <
(or >
) instead of <>
.您可以决定始终在 p1 端拥有按字典顺序排列的第一个(或最后一个,为了论证)国家 - 使用
<
(或>
)而不是<>
。 Also, note that your where
clause is redundant, since you already have this condition in the on
clause of the join
:另外,请注意您的
where
子句是多余的,因为您已经在join
的on
子句中具有此条件:
select p1.country,
p2.country,
p1.population,
p2.population
from pops p1
inner join pops p2
on p1.population between p2.population - 100 and p2.population + 100 and
p1.country < p2.country
-- Here ------------------^
just change the join condition of p1.country <> p2.country to p1.country < p2.country只需将 p1.country <> p2.country 的连接条件更改为 p1.country < p2.country
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.