mysql select for simple join for rows having certain cells with duplicate values

标签 mysql sql join

我正在尝试对两个表执行 MySQL SELECT,结果仅当有多个行且其中两个列具有重复值时才显示每个表中的列。

以下 SELECT 语句为客户下的所有订单提供 customer_id、order_id、product_ids 和 skus...

select orders2.customer_id, items2.order_id, items2.product_id, items2.sku 
from orders orders2, order_items items2
where orders2.status = 'complete' 
    and orders2.customer_id is not null
    and orders2.entity_id = items2.order_id
order by orders2.customer_id ASC, items2.product_id ASC;

给我结果......

+-------------+----------+------------+--------------------------+
| customer_id | order_id | product_id | sku                      |
+-------------+----------+------------+--------------------------+
|       29813 |    38025 |        306 | BB_MAT101                |
|       29813 |    38027 |        309 | BB_MAT250                |
|       29814 |    28844 |        302 | BB_ENG101                |
|       29814 |    27615 |        384 | BB_MS-ACC101             |
|       29814 |    27616 |        385 | BB_MS-ACC102             |
|       29814 |    27615 |        385 | BB_MS-ACC102             |
|       29814 |    27614 |        409 | BB_MS-MAT101             |
|       29814 |    27584 |        410 | BB_MS-MAT150             |
|       29815 |    27592 |        384 | BB_MS-ACC101             |
|       29815 |    27593 |        384 | BB_MS-ACC101             |
|       29815 |    27594 |        384 | BB_MS-ACC101             |
|       29815 |    27599 |        385 | BB_MS-ACC102             |
|       29815 |    27592 |        402 | BB_MS-ECON101            |
|       29815 |    27593 |        402 | BB_MS-ECON101            |
|       29815 |    27594 |        402 | BB_MS-ECON101            |
|       29815 |    27596 |        403 | BB_MS-ECON102            |
|       29815 |    27598 |        404 | BB_MS-ENG099             |
|       29815 |    27588 |        405 | BB_MS-ENG101             |
|       29815 |    27595 |        406 | BB_MS-ENG102             |
|       29815 |    27589 |        408 | BB_MS-MAT099             |
|       29815 |    27585 |        409 | BB_MS-MAT101             |
|       29815 |    27589 |        410 | BB_MS-MAT150             |
|       29815 |    27589 |        411 | BB_MS-MAT201             |
+-------------+----------+------------+--------------------------+

以下 SELECT 让我可以确定客户多次订购产品的情况...

select orders1.customer_id as dupe_customer_id, items1.product_id as dupe_product_id, count(*) as duplicates
from orders orders1, order_items items1
where orders1.status = 'complete' 
    and orders1.customer_id is not null
    and orders1.entity_id = items1.order_id
group by orders1.customer_id, items1.product_id
having duplicates > 1;

结果...

+------------------+-----------------+------------+
| dupe_customer_id | dupe_product_id | duplicates |
+------------------+-----------------+------------+
|            29814 |             385 |          2 |
|            29815 |             384 |          3 |
|            29815 |             402 |          3 |
+------------------+-----------------+------------+

我想弄清楚的是如何组合这些,以便我只在第一个选择中获得满足第二个选择条件的那些项目,这样输出就像...

+-------------+----------+------------+--------------------------+
| customer_id | order_id | product_id | sku                      |
+-------------+----------+------------+--------------------------+
|       29814 |    27616 |        385 | BB_MS-ACC102             |
|       29814 |    27615 |        385 | BB_MS-ACC102             |
|       29815 |    27592 |        384 | BB_MS-ACC101             |
|       29815 |    27593 |        384 | BB_MS-ACC101             |
|       29815 |    27594 |        384 | BB_MS-ACC101             |
|       29815 |    27592 |        402 | BB_MS-ECON101            |
|       29815 |    27593 |        402 | BB_MS-ECON101            |
|       29815 |    27594 |        402 | BB_MS-ECON101            |
+-------------+----------+------------+--------------------------+

请注意,结果集的主要标准是 customer_id 和 product_id 的任何组合必须出现不止一次。

我正在为如何将它们结合起来而苦苦挣扎。我的尝试导致一个(或多个)列在结果的每一行中重复出现不正确的数据。

我在执行这种类型的 SELECT 时用我的 MySQL 知识碰壁了,过去一个小时的搜索没有产生任何结果。

最佳答案

试试这样的东西:

SELECT orders.customer_id
    ,items.order_id
    ,items.product_id
    ,items.sku 
FROM orders AS orders
INNER JOIN order_items AS items ON orders.entity_id = items.order_id
WHERE (orders.customer_id,items.product_id) IN (
    SELECT orderSub.customer_id
        ,itemSub.product_id
    FROM orders AS orderSub
    INNER JOIN order_items AS itemSub ON orderSub.entity_id = itemSub.order_id
    WHERE orderSub.status = 'complete' 
        AND orderSub.customer_id IS NOT NULL
    GROUP BY orderSub.customer_id
        ,itemSub.product_id
    HAVING COUNT(*) > 1;
);

它将查询作为多列匹配的子查询提供给 IN 子句使用。这超出了我的考虑范围,因为我不确定 MySQL 是否支持这种语法,但它适用于 Teradata 和 DB2。

出于性能原因,我还转换了您的查询以最大限度地符合 ANSI,因此如果它看起来与 WHERE IN 子句不同,这就是原因。

关于mysql select for simple join for rows having certain cells with duplicate values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864761/

相关文章:

mysql - 在 mysql 中以随机顺序从 2 个表中选择 2 个不同的记录

sql - 更改没有主键的表中的每条记录?

mysql - SQL 使用最大值连接两个表

javascript - 如何将变量值从 javascript 传递到 perl

linux - 使用内连接连接两个 TSV 文件

Sqlite:查询计算某行在 'through'表中被引用的次数

python - python fetchall()中的MySQL死锁

mysql - 导入错误 : No module named MySQLdb in Zope

php - 如何只选择每个类别的一条记录?

sql - 从 Select 语句到 Delete