我正在撰写查询,以通过其产品ID查找订单的完全重复。
找到重复的条件是:
1)订单具有相同的产品数量。
2)所有产品ID都相同。
尝试过这样的事情,但它没有成功:
SELECT
order.*,
count(same_products.id),
count(all_products.id)
FROM orders
LEFT JOIN products AS all_products ON all_products.order_id = orders.id
LEFT JOIN products AS same_products
ON same_products.order_id = orders.id AND same_products.id IN (30868, 30862)
GROUP BY orders.id
HAVING count(same_products.id) = 4 AND count(all_products.id = 4)
答案 0 :(得分:0)
如果你想要计算重复的行,你应该避免使用所有列(*)选择器,因为如果你的列中有增量值,这不会让你找到重复的行。
SELECT
order.id
count(same_products.id),
count(all_products.id)
FROM orders
LEFT JOIN products AS all_products ON all_products.order_id = orders.id
LEFT JOIN products AS same_products
ON same_products.order_id = orders.id AND same_products.id IN (30868, 30862)
GROUP BY orders.id
HAVING count(same_products.id) >1 OR count(all_products.id )> 1
对于重复的行,您应该检查计数> 1(对于两个计数) 并且要小心count(all_products.id = 4)如果你需要过滤这个值,你应该将它添加到相关表的on条件中,例如:
SELECT
order.id
count(same_products.id),
count(all_products.id)
FROM orders
LEFT JOIN products AS all_products ON all_products.order_id = orders.id and all_products.id =4
LEFT JOIN products AS same_products
ON same_products.order_id = orders.id AND same_products.id IN (30868, 30862)
GROUP BY orders.id
HAVING count(same_products.id) >1
答案 1 :(得分:0)
我不清楚你的副本是什么意思,我从你的描述中假设它是2个相同产品的订单。这似乎有点简单,例如给定
MariaDB [sandbox]> select * from orders;
+------+---------------------+-------------+
| id | order_created | customer_id |
+------+---------------------+-------------+
| 1 | 2016-01-01 00:00:00 | 1 |
| 2 | 2016-02-01 00:00:00 | 1 |
| 3 | 2016-03-01 00:00:00 | 1 |
| 4 | 2016-01-01 00:00:00 | 2 |
| 5 | 2016-02-01 00:00:00 | 2 |
| 6 | 2016-01-01 00:00:00 | 3 |
| 10 | 2016-12-01 00:00:00 | 4 |
+------+---------------------+-------------+
7 rows in set (0.00 sec)
MariaDB [sandbox]> select * from order_details;
+----+---------+-----------+------+
| id | orderid | productid | qty |
+----+---------+-----------+------+
| 1 | 1 | 1213 | 10 |
| 2 | 1 | 9999 | 10 |
| 3 | 2 | 8888 | 10 |
| 4 | 3 | 1213 | 10 |
| 5 | 4 | 2222 | 10 |
| 6 | 5 | 9999 | 30 |
| 7 | 5 | 1213 | 30 |
| 8 | 6 | 9999 | 30 |
| 9 | 6 | 1213 | 30 |
+----+---------+-----------+------+
9 rows in set (0.00 sec)
select orders1.*,orders2.*,t.*
from
(select * from
(
select orderid o1orderid,group_concat(productid order by productid) o1grp,sum(qty) qty1
from order_details
group by orderid
) o1
join
(select orderid o2orderid,group_concat(productid order by productid) o2grp, sum(qty) qty2
from order_details
group by orderid
) o2
on o2grp = o1grp and qty2 = qty1 and o2orderid > o1orderid
) t
join orders orders1 on t.o1orderid = orders1.id
join orders orders2 on t.o2orderid = orders2.id
返回
+------+---------------------+-------------+------+---------------------+-------------+-----------+-----------+-------+-----------+-----------+-------+
| id | order_created | customer_id | id | order_created | customer_id | o1orderid | o1grp | qty1 | o2orderid | o2grp | qty2 |
+------+---------------------+-------------+------+---------------------+-------------+-----------+-----------+-------+-----------+-----------+-------+
| 5 | 2016-02-01 00:00:00 | 2 | 6 | 2016-01-01 00:00:00 | 3 | 5 | 1213,9999 | 30,30 | 6 | 1213,9999 | 30,30 |
+------+---------------------+-------------+------+---------------------+-------------+-----------+-----------+-------+-----------+-----------+-------+
1 row in set (0.03 sec)
但客户数量不同。