【问题标题】:MySQL select record from one table there not exist in othreMySQL从一张表中选择记录,其他表中不存在
【发布时间】:2012-05-25 19:31:22
【问题描述】:

我有 2 张桌子:

TABLE customer_service_provider
==================================
id   | customer | service_provider
==================================
1    | 1        | 1
2    | 1        | 2
3    | 1        | 3
4    | 2        | 1
5    | 2        | 2
6    | 3        | 1
7    | 4        | 1
8    | 4        | 2
9    | 4        | 3
===================================

TABLE service_provider
======================
id     | Name
======================
1      | Company1
2      | Company2
3      | Company3
======================

我需要从表customer_service_provider(字段customerservice_provider)获取信息,其中service_provider 不存在于表customer_service_provider 中,但存在于service_provider 表中。

结果应该是这样的:

customer   |  service_provider
==============================
2          | 3
3          | 2
3          | 3
==============================

已解决:

选择 不同的 sp.id, csp.customer 从 service_provider sp, customer_service_provider csp 在哪里 sp.id 不在(选择 csp2.service_provider FROM customer_service_provider csp2 WHERE csp2.customer = csp.customer)

【问题讨论】:

  • 我不明白。你到底想要什么?
  • 您在customer_service_provider 中确实有service_provider 1, 2, 3...所以根据您的要求,您应该有no 输出
  • 不要放置一个巨大的“已解决”部分。要么接受下面解决您的问题的答案,或者如果您自己做,发布答案并将其标记为已接受的答案。

标签: mysql select not-exists


【解决方案1】:
选择 不同的 sp.id, csp.customer 从 service_provider sp, customer_service_provider csp 在哪里 sp.id 不在(选择 csp2.service_provider FROM customer_service_provider csp2 其中 csp2.customer = csp.customer)

【讨论】:

    【解决方案2】:

    试试这个:

    SELECT c.customer, s.id
    FROM customer_service_provider c, service_provider s
    WHERE NOT EXISTS (
        SELECT * FROM customer_service_provider c2
        WHERE c2.customer = c.customer AND c2.service_provider = s.id
    )
    

    或者,更高效:

    SELECT c.customer, s.id
    FROM customer_service_provider c, service_provider s
    LEFT OUTER JOIN customer_service_provider c2 ON c2.customer = c.customer AND c2.service_provider = s.id
    WHERE c2.id IS NULL
    

    我没有测试过,请告诉我。

    【讨论】:

    • 查询永远执行,只是挂起,没有结果。 C 表有大约 16k 条记录,S 表只有 10 条,所以非常大的循环来了。
    • 好的,我没有考虑表格大小。我尝试重写一个更高效的版本。
    • 抱歉这样的延迟,但它不起作用:[SQL] SELECT c.customer, s.id FROM customer_service_provider c, service_provider s LEFT OUTER JOIN customer_service_provider c2 ON c2.customer = c.customer AND c2 .service_provider = s.id WHERE c2.id IS NULL [Err] 1054 - Unknown column 'c.customer' in 'on Clause' 有趣的是,该列存在,但抛出了那种错误,我试图重写它有多种方式,但没有帮助。
    • 第一个查询完美运行,只需要添加DISTINCT,而我忘记的MOST重要的是,将索引添加到表中,非常感谢!跨度>
    【解决方案3】:

    我想您也有一张与客户共进的桌子。在这种情况下,请使用以下内容:

    select customer.id, service_provider.id 
    from customer, service_provider 
    left join customer_service_provider on customer.id=customer_service_provider.customer and service_provider.id=customer_service_provider.service_provider
    where customer_service_provider.id IS NULL;
    

    基本上,返回客户和服务提供商的所有组合。在 customer_service_provider 表上做一个LEFT JOIN;并且只保留没有匹配记录的东西。

    【讨论】:

    • 以下查询:SELECT add_customers.id, service_provider.id FROM add_customers, service_provider LEFT JOIN customer_service_provider ON add_customers.id=customer_service_provider.customer AND service_provider.id=customer_service_provider.service_provider WHERE customer_service_provider.id IS NULL 返回此错误:1054 - Unknown column 'add_customers.id' in 'on Clause'
    • 嗨 Arturas,你的表 'add_customers' 中的 id 字段的名称是什么??
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 2019-05-23
    • 2020-06-10
    • 2011-02-10
    • 2020-06-08
    • 2013-05-19
    相关资源
    最近更新 更多