sql查询一个主表不存在子表的记录(子表带条件),这个怎么查询?

it0_soft 2020-03-30 02:35:14
sql多表查询,我想查询主表不存在于子表的记录,之前写的sql查询:

select * from [Client] where (select count(1) as num from Records where Records.cID = Client.cID) = 0 and (Select count(1) as nums from Hetong where Hetong.cId = Client.cId) = 0 and cYn = 1 and datediff(d,cLastUpdated,getdate()) > = 5 order by cDate desc

现在我要更改需求,除了查询主表不存在于子表记录外,还要给子表带上查询条件,那么我想查询前面的条件外再加一个条件就是子表的hState记录,这个能实现吗?
...全文
770 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
it0_soft 2020-03-31
  • 打赏
  • 举报
回复
谢谢两位的耐心解答
RINK_1 2020-03-30
  • 打赏
  • 举报
回复

select * from [Client] 
where ((not exists (select 1 from Records where Records.cID = Client.cID)
and not exists (Select 1 from Hetong where Hetong.cId = Client.cId))
or exists (Select 1 from Hetong where Hetong.cId = Client.cId and hState=XXXX))
and cYn = 1 and datediff(d,cLastUpdated,getdate()) > = 5 order by cDate desc

Hello World, 2020-03-30
  • 打赏
  • 举报
回复
这样?
SELECT      *
FROM [Client] c
WHERE NOT EXISTS (SELECT 1 FROM Records r WHERE r.cID = c.cID)
AND NOT EXISTS (SELECT 1 FROM Hetong h
WHERE h.cId = c.cId AND h.hState <> '没消费' --没消费的在这里排除掉,最终结果列出来
) AND cYn = 1
AND DATEADD(DAY, -5, GETDATE()) >= cLastUpdated
ORDER BY cDate DESC;
it0_soft 2020-03-30
  • 打赏
  • 举报
回复
是这样的我再具体描述一下,主表Client,子表Records(记录表),Hetong(消费表),起初我是需要查询主表不存在这两个子表的记录,但是现在我要改变查询条件,Hetong(消费表)里面存在一个hState字段值是:没消费,没买单,没确认,就是我要把存在于表Hetong中字段hState满足于条件的记录也查出来,总的意思就是不存在于表Hetong和存在于表Hetong并满足某些条件的记录查询出来。我怎么感觉这是绝路啊。
it0_soft 2020-03-30
  • 打赏
  • 举报
回复
是这样的我再具体描述一下,主表Client,子表Records(记录表),Hetong(消费表),起初我是需要查询主表不存在这两个子表的记录,但是现在我要改变查询条件,Hetong(消费表)里面存在一个hState字段值是:没消费,没买单,没确认,我也要顺带把这个条件查询出来。
引用 5 楼 Hello World, 的回复:
不明白,举个例子,源数据是什么样子,得到什么结果?
Hello World, 2020-03-30
  • 打赏
  • 举报
回复
不明白,举个例子,源数据是什么样子,得到什么结果?
it0_soft 2020-03-30
  • 打赏
  • 举报
回复
可能是我表述错了吧,我想查询这两个不存在于子表的数据,但是存在于子表Hetong里面某一特定的数据可以查出来
it0_soft 2020-03-30
  • 打赏
  • 举报
回复
引用 1 楼 Hello World, 的回复:
当然可以加,你想在哪个子表上加就在哪个子表上加就好了,比如:
select * from [Client] where 
(select count(1) as num from Records where Records.cID = Client.cID) = 0 
and (Select count(1) as nums from Hetong where Hetong.cId = Client.cId and Hetong.hState=1) = 0 
and cYn = 1 and datediff(d,cLastUpdated,getdate()) > = 5 
order by cDate desc
这种我早上试过了,就是给Hetong这个子表加一个带条件的查询,没有查出来对应的数据。
Hello World, 2020-03-30
  • 打赏
  • 举报
回复
还有,可以改为exists判断,条件中不要对日期列进行计算
select * from [Client] c where 
not exists (select 1 from Records r where r.cID = c.cID)
and not exists (Select 1 from Hetong h where h.cId = c.cId and h.hState=1)
and cYn = 1 and dateadd(day,-5,getdate())>=cLastUpdated
order by cDate desc
Hello World, 2020-03-30
  • 打赏
  • 举报
回复
当然可以加,你想在哪个子表上加就在哪个子表上加就好了,比如:
select * from [Client] where 
(select count(1) as num from Records where Records.cID = Client.cID) = 0
and (Select count(1) as nums from Hetong where Hetong.cId = Client.cId and Hetong.hState=1) = 0
and cYn = 1 and datediff(d,cLastUpdated,getdate()) > = 5
order by cDate desc

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧