查找已多次订购的订单(SQL查询)

时间:2017-03-13 15:59:47

标签: mysql sql

我试图找出哪些订单不止一次。这将帮助我确定最常见的订单。这是我的订单表,其中包含一些虚拟数据;

表名:订单

OrderID | ProductID | Quantity -------- | ----------| -------- 1 | 3 | 6 | 1 | 6 | 3 | 2 | 5 | 1 | 3 | 10 | 9 | 3 | 9 | 1 | 3 | 3 | 2 | 4 | 6 | 3 | 4 | 3 | 6 |

正如您所看到的,订单1和订单4是相同的订单,我需要一个查询来帮助我识别这些订单。

2 个答案:

答案 0 :(得分:0)

你可以使用一个分组,并且有这样的

SELECT OrderID, ProductID, Quantity, Count(*) as Count
FROM Your_table_name_which_you_did_not_tell_us
GROUP BY OrderID, ProductID, Quantity
HAVING COUNT(*) > 1

答案 1 :(得分:0)

仅供参考,我为我做了这份工作;

"SELECT MIN(orderID) as orderId, COUNT(*) as TimesOrdered" + " FROM ORDERS" + " GROUP BY partId, quantity" + " HAVING COUNT(*)>1";