gpt4 book ai didi

使用 NOT EXISTS 的 SQL

转载 作者:行者123 更新时间:2023-12-05 09:23:59 26 4
gpt4 key购买 nike

我正在尝试编写一个 SQL 查询,返回自 4 月 1 日以来收到新发票但尚未安排今年秋季交货的客户的所有学生电子邮件地址。即使我知道有满足这些条件的条目,这也会返回一个空集。我尝试了几种不同的方法,但都没有成功,有没有办法做到这一点?

SELECT clients.studentEmail 
FROM `clients`, `invoices`
WHERE clients.clientId = invoices.clientId
AND invoices.datePosted > "2013-04-01"
AND NOT EXISTS
(SELECT *
FROM appointments, clients
WHERE clients.clientId = appointments.clientId
AND appointments.serviceDirection = "Delivery"
AND appointments.date > '2013-07-01')

最佳答案

您必须将您的 not exists 子查询与外部查询相关联。例如:

select  clients.studentemail 
from clients c
join invoices i
on c.clientid = i.clientid
where invoices.dateposted > "2013-04-01"
and not exists
(
select *
from appointments a
where c.clientid = a.clientid -- Relates outer to inner query
and a.servicedirection = "delivery"
and a.date > '2013-07-01')
)

关于使用 NOT EXISTS 的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738657/

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