MySQL JoinWhere 关联不存在

发布于 2024-12-10 19:04:57 字数 287 浏览 0 评论 0原文

我获得了 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 年没有任何预约的客户。对如何去做感到困惑。

I get customer ids for customers with sites that have appointments in 2011

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   

I'm looking to get customers who don't have any appointments in 2011. Confused on how to go about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈甜 2024-12-17 19:04:57

有几种方法:

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

A couple of ways:

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
    )

or:

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
蓝天 2024-12-17 19:04:57

...哪里不是年份(约会.日) = 2011

...WHERE NOT YEAR(appointments.day) = 2011

灼痛 2024-12-17 19:04:57

应该做一个答案而不是编辑。由于缩放,不想真正寻找 NULL。

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

Should have done an answer instead of an edit. Don't really want to look for NULL's due to scaling.

SELECT id
FROM customers 
WHERE NOT EXISTS (SELECT * FROM sites 
                      INNER JOIN appointments ON appointments.site_id = sites.id
                  WHERE sites.customer_id = customers.id AND YEAR(appointments.day) = 2011)
樱桃奶球 2024-12-17 19:04:57
 SELECT customers.id
 FROM customers, sites, appointments
 WHERE (customers.id = sites.customer_id and appointments.id = sites.id) 
 and  YEAR(appointments.day) <> 2011

请检查您的特定数据库服务器是否有“<>”这是“不等于”。

 SELECT customers.id
 FROM customers, sites, appointments
 WHERE (customers.id = sites.customer_id and appointments.id = sites.id) 
 and  YEAR(appointments.day) <> 2011

Please check your specific database server for "<>" which is "not equal".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文