gpt4 book ai didi

mysql - SQL查询类似于distinct

转载 作者:行者123 更新时间:2023-11-29 18:17:51 25 4
gpt4 key购买 nike

给定一个表 (c1, c2),其值为 (1,1) (1,4)、(4,1) (2,2),什么查询允许我只输出 c2 =1 的结果并且表中只存在一个条目?在这种情况下,它只会是 (4,1),因为有两个条目 c1 =1。干杯。

最佳答案

嗯..有很多方法。这是一个:

select c1, c2
from my_table t1
where c2 = 1
and 1 = (select count(*) from my_table t2 where t2.c1 = t1.c1)

其他一些方法:

select t1.c1, t1.c2
from my_table t1
join my_table t2 using (c1)
where t1.c2 = 1
group by t1.c1, t1.c2
having count(*) = 1;
select c1, c2
from my_table t1
where c2 = 1
and not exists (
select *
from from my_table t2
where t2.c1 = t1.c1
and t2.c2 <> t1.c2
);
select t1.c1, t1.c2
from my_table t1
left join my_table t2
on t2.c1 = t1.c1
and t2.c2 <> t1.c2
where t1.c2 = 1
and t2.c1 is null;

最后两个查询要求表中没有重复项。

关于mysql - SQL查询类似于distinct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46864935/

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