2 回答
TA贡献1872条经验 获得超3个赞
您可以使用这些功能least(),greatest()因为点是可以互换的:
select t.point1, t.point2, t.medium
from (
select *,
row_number() over(
partition by least(point1, point2), greatest(point1, point2)
order by field(medium, 'air', 'train', 'road')
) rn
from connections_tbl
) t
where t.rn = 1
结果:
| point1 | point2 | medium |
| ------- | ------ | ------ |
| baglung | palpa | road |
| baglung | pkr | train |
| ktm | brt | air |
| ktm | pkr | air |
TA贡献1884条经验 获得超4个赞
您可以使用相关子查询进行过滤:
select c.*
from connections_tbl c
where c.medium = (
select c1.medium
from connections_tbl c1
where c1.point1 = c.point1 and c1.point2 = c.point2
order by field(medium, 'air', 'train', 'road')
limit 1
) t
或者,在 MySQL 8.0 中,您可以使用row_number():
select point1, point2, medium
from (
select
c.*,
row_number() over(partition by point1, point2 order by field(medium, 'air', 'train', 'road')) rn
from connections_tbl c
) t
where rn = 1
- 2 回答
- 0 关注
- 96 浏览
添加回答
举报