简体   繁体   English

MYSQL:如何 Select 购买了一种产品但未购买另一种产品的客户

[英]MYSQL: How to Select Customers That Bought One Product But Not Another

I am working on a database and I would like to get the list of all the customers that bought a product with a name containing citroen but none with a name containing guzzi.我正在研究一个数据库,我想获取所有购买了名称包含 citroen 但没有名称包含 guzzi 的产品的客户的列表。

The structure of the database is the following:数据库的结构如下:

数据库

I have tried the following:我尝试了以下方法:

SELECT customerName, productName
FROM customers c
INNER JOIN orders o
USING (customerNumber)
INNER JOIN orderdetails od
USING (orderNumber)
INNER JOIN products p
USING (productCode)
WHERE productName LIKE '%citroen%'
AND productName NOT IN (
SELECT productCode
FROM products
WHERE productName LIKE '%guzzi%'
);

but it selects also the one who bought guzzi.但它也选择了购买 guzzi 的人。

Any clue?有什么线索吗?

Conditional aggregation to count how many products of each type were purchased...条件聚合计算每种类型的产品购买了多少...

SELECT
  o.customerNumber
FROM
  orders o
INNER JOIN
  orderdetails od
    USING (orderNumber)
INNER JOIN
  products p
    USING (productCode)
WHERE
     p.productName LIKE '%citroen%'
  OR p.productName LIKE '%guzzi%'
GROUP BY
  o.customerNumber
HAVING
      MAX(CASE WHEN p.productName LIKE '%citroen%' THEN 1 ELSE 0 END) = 1
  AND MAX(CASE WHEN p.productName LIKE '%guzzi%'   THEN 1 ELSE 0 END) = 0

Longer, but can be faster if each customer has (on average) a very large number of relevant purchases...更长,但如果每个客户(平均而言)大量相关购买,则可以更快......

SELECT
  *
FROM
  customer
WHERE
  EXISTS (
    SELECT
      o.customerNumber
    FROM
      orders o
    INNER JOIN
      orderdetails od
        USING (orderNumber)
    INNER JOIN
      products p
        USING (productCode)
    WHERE
          o.customerNumber  =   customer.CustomerNumber
      AND p.productName    LIKE '%citroen%'
  )
  AND NOT EXISTS (
    SELECT
      o.customerNumber
    FROM
      orders o
    INNER JOIN
      orderdetails od
        USING (orderNumber)
    INNER JOIN
      products p
        USING (productCode)
    WHERE
          o.customerNumber  =   customer.CustomerNumber
      AND p.productName    LIKE '%guzzi%'
  )

In your last condition, you're comparing productName with productCode, so I suppose the NOT IN will always return TRUE.在您的最后一个条件中,您将 productName 与 productCode 进行比较,所以我想 NOT IN 将始终返回 TRUE。

SELECT customerName, productName
FROM customers c
    INNER JOIN orders o
        USING (customerNumber)
    INNER JOIN orderdetails od
        USING (orderNumber)
    INNER JOIN products p
        USING (productCode)
WHERE productName LIKE '%citroen%'
AND NOT EXISTS (
    SELECT 'x'
    FROM orders o2
        INNER JOIN orderdetails od2
            USING (orderNumber)
        INNER JOIN products p2
            USING (productCode)
    WHERE o2.customerNumber = c.customerNumber
    AND p2.productName LIKE '%guzzi%'
)
AND productName NOT LIKE '%guzzi%'
;```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让购买了 x 产品的客户也购买了 y 产品 - How to get the customers who bought x product also bought y product 是否有查询选择哪些客户购买了特定产品以及这些客户购买了哪些其他产品? - Is there a query which select which customers have bought a specific product and what other products did these same customers buy? MySQL:如何选择购买带有附加附件产品的客户 - MySQL: How to select customers who purchased a product with addon accessory 查找客户尚未购买的产品的平均评分 - Finding average ratings of a product that customers have not bought 未购买的 MySQL 查询客户 - MySQL Query Customers Who Have Not Bought MySQL找到购买了X但没有购买Y的客户 - Mysql find customers that bought X but not Y 查询客户与所列产品一起购买的产品 - query for what customers have bought together with the listed product 选择所有客户,除非他们有其他产品-SQL - Select all customers except if they have another product - SQL 向客户展示2018年第一次购买前几年从未购买过的产品系列的SQL语句 - SQL statement to show customers that in 2018 bought a product series for the first time that they never bought in the previous years before 对于每个客户,请选择购买相同商品的所有其他客户 - For every customer select all the other customers that bought the same item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM