gpt4 book ai didi

sql - 选择唯一的列组合

转载 作者:行者123 更新时间:2023-12-04 04:41:27 25 4
gpt4 key购买 nike

我有下表:

create table cric_country
(
id int,
country varchar (20)
)
insert into cric_country values
(1,'Ind'),(2,'Aus'),(3,'NZ')

我需要为我需要 2 列国家的国家获得比赛装置,并且应该省略相同的国家和重复的比赛:
我写了以下查询:
select
t1.country,t2.country
from cric_country t1
inner join
cric_country t2
on t1.country <> t2.country

但它没有解决目的,因为我有 Aus/Ind 以及 Ind/Aus,需要省略一个但不能做一个明确的。

最佳答案

SELECT a.country country1, b.country country2
FROM cric_country a
CROSS JOIN cric_country b
WHERE a.country < b.country
ORDER BY a.country, b.country
  • SQLFiddle Demo

  • 输出
    ╔══════════╦══════════╗
    ║ COUNTRY1 ║ COUNTRY2 ║
    ╠══════════╬══════════╣
    ║ Aus ║ Ind ║
    ║ Aus ║ NZ ║
    ║ Ind ║ NZ ║
    ╚══════════╩══════════╝

    关于sql - 选择唯一的列组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823480/

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