gpt4 book ai didi

mysql - 带有子查询的 SQL 查询

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

我的数据是这样的:

data1_qqq_no_abc_ccc
data1_qqq_abc_ccc
data2_qqq_no_abc_ccc
data2_qqq_abc_ccc
data3_qqq_no_abc_ccc
data4_qqq_no_abc_ccc
data4_qqq_abc_ccc

...

现在我想获取数据具有子字符串 _no_abc_ccc 但没有 _abc_ccc 的字段。在上面的例子中,它的data3

我正在尝试为它创建一个查询。粗略的是

select SUBSTRING_INDEX(name, 'abc', 1)  
from table1
where SUBSTRING_INDEX(name, 'abc', 1) not LIKE "%no"
and NOT IN (select SUBSTRING_INDEX(name, '_no_abc', 1)
from table
where name LIKE "%no_abc");

最佳答案

像这样(?)

create table t (
col text
);

insert into t
values
('data1_qqq_no_abc_ccc'),
('data1_qqq_abc_ccc'),
('data2_qqq_no_abc_ccc'),
('data2_qqq_abc_ccc'),
('data3_qqq_no_abc_ccc'),
('data4_qqq_no_abc_ccc'),
('data4_qqq_abc_ccc');

select f from (
select SUBSTRING_INDEX(col, '_', 1) as f, SUBSTRING_INDEX(col, '_', -3) as s from t
) tt
group by f
having
count(case when s = 'no_abc_ccc' then 1 end) > 0
and
count(case when s like '%qqq_abc%' then 1 end) = 0

demo

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

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