gpt4 book ai didi

MySQL Join Where关联不存在

转载 作者:太空宇宙 更新时间:2023-11-03 11:12:03 25 4
gpt4 key购买 nike

我为在 2011 年有约会的网站的客户获取客户 ID

SELECT customers.id
FROM customers
INNER JOIN sites ON customers.id = sites.customer_id
INNER JOIN appointments ON appointments.site_id = sites.id
WHERE YEAR(appointments.day) = 2011

我正在寻找在 2011 年没有任何约会的客户。对如何去做感到困惑。

最佳答案

几种方式:

SELECT
C.id
FROM
Customers C
WHERE
NOT EXISTS (
SELECT *
FROM
Sites S
INNER JOIN Appointments A ON
A.site_id = S.id AND
YEAR(A.day) = 2011
WHERE
S.customer_id = C.id
)

或:

SELECT DISTINCT
C.id
FROM
Customers C
INNER JOIN Sites S ON
S.customer_id = C.id
LEFT OUTER JOIN Appointments A ON
A.site_id = S.id AND
YEAR(A.day) = 2011
WHERE
A.id IS NULL

关于MySQL Join Where关联不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840856/

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