gpt4 book ai didi

mysql - MySQL 中的循环和 if

转载 作者:行者123 更新时间:2023-11-29 12:24:54 24 4
gpt4 key购买 nike

表中有几列,一列是名为“Country”的 varchar 列,另一列是名为“SomeFlag”的 bool 列,我想从记录中过滤所有“SomeFlag”同时为 0 和 1 的国家/地区.
例如,

Record1: country is US, SomeFlag is 0 and some other values.  
Record2: country is US, SomeFlag is 1 and some other values.
Record3: country is CA, SomeFlag is 1 and some other values.

那么 US 就是我想要过滤掉的内容,我应该如何构建这个 SQL 查询?
提前致谢。

最佳答案

select Country from T
group by Country
having min(cast(SomeFlag as int)) = max(cast(SomeFlag as int))

select Country from T
group by Country
having count(distinct SomeFlag) = 1

select Country from T
where not exists (
select 1 from T
where SomeFlag = 0
) or not exists (
select 1 from T
where SomeFlag = 1
)

select Country from T
where Country not in (
select Country from T
group by Country
having
min(cast(SomeFlag as int)) = 0
and max(cast(SomeFlag as int)) = 1
)

SomeFlag 中的空值会起作用吗?

关于mysql - MySQL 中的循环和 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471876/

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