gpt4 book ai didi

mysql - 在 SQL 中比较同一个表中的两行,一列相等,另一列组合不相等

转载 作者:行者123 更新时间:2023-11-29 18:00:58 28 4
gpt4 key购买 nike

我的数据库中有下表:

表名称:保险表

ID | Policy | Lon | Lat

1 | 34564 | 2.0 | 4.0

2 | 67548 | 1.1 | 1.4

3 | 34564 | 1.8 | 9.4

4 | 98271 | 4.3 | 2.3

5 | 90198 | 5.6 | 4.5

6 | 98271 | 1.3 | 5.6

7 | 90198 | 5.6 | 4.5

8 | 34564 | 2.0 | 4.0

我正在寻找一个 sql 查询,它将按以下方式返回结果集:结果集包含策略值至少等于其他行的行,但其他行的 (Lon, Lat) 组合值应该不同。

对于上表,我应该得到以下结果集:

1 | 34564 | 2.0 | 4.0 

3 | 34564 | 1.8 | 9.4

4 | 98271 | 4.3 | 2.3

6 | 98271 | 1.3 | 5.6

非常感谢有关如何编写此查询的回复。

最佳答案

您可以使用存在:

select t.*
from insurancetable t
where exists (select 1 from insurancetable t2 where t2.policy = t.policy and t2.id <> t.id);

编辑:

根据你的问题,id应该没问题。但如果您愿意,您可以使用纬度和经度:

select t.*
from insurancetable t
where exists (select 1
from insurancetable t2
where t2.policy = t.policy and
(t2.lat <> t.lat or t2.lon <> t.lon)
);

关于mysql - 在 SQL 中比较同一个表中的两行,一列相等,另一列组合不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48352168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com