gpt4 book ai didi

Mysql:如何获得给定条件的逆结果?

转载 作者:行者123 更新时间:2023-12-01 00:34:55 24 4
gpt4 key购买 nike

我编写了一个查询以获得与我的查询生成的结果完全相反的结果。

查询 1:

select count(*) from DB.titlecard tt 
join _date d on d.td_id = tt.td_id
join sometask s on s.s_id = tt.s_id and d.em_id = s.em_id
where d.`date` > "2019-01-01";

它为我提供了 21354 个结果。

查询 2:(没有任务连接的类似查询)

select count(*) from DB.titlecard tt 
join _date d on d.td_id = tt.td_id
where d.`date` > "2019-01-01";

产生 28984

我需要查询 2 与查询 1 不同的记录

像这样的,但它提供了 1000 000 条记录。

select count(*) from DB.titlecard tt 
join _date d on d.td_id = tt.td_id
join sometask s on s.s_id <> tt.s_id and d.em_id = s.em_id
where d.`date` > "2019-01-01";

我知道通过比较两个查询来获得差异。

但我正在单个查询中寻找更好的方法(因为我在查询 1 中只添加了一个表)

仅这一点就不同了,查询 1:

join sometask s on s.s_id = tt.s_id

我要申请的条件,

join sometask s on s.s_id <> tt.s_id

最佳答案

如果您更改 JOINLEFT JOIN然后只选择 s.id 所在的行是NULL你会得到你想要的计数:

select count(*) from DB.titlecard tt 
join _date d on d.td_id = tt.td_id
left join sometask s on s.s_id = tt.s_id and d.em_id = s.em_id
where d.`date` > "2019-01-01" and s.s_id IS NULL;

关于Mysql:如何获得给定条件的逆结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55921718/

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