一些背景:
我正在嘗試在另一個表中顯示所有數量最多的專案。
我想在 OrderItems 表中顯示按“數量”排序的暢銷商品。數量為 '5' 的 productId 應該首先出現,然后是產品數量 '4'.. 以此類推。
我只能檢索與 productId、尺寸和顏色相匹配的專案,但我不知道如何計算數量。
訂單項表
| orderItemsId | 訂單號 | 產品編號 | 數量 | 尺寸 | 顏色 |
|---|---|---|---|---|---|
| 1 | 48 | 1 | 1 | 小的 | 黑色的 |
| 2 | 48 | 2 | 2 | 小的 | 黑色的 |
| 3 | 48 | 3 | 5 | 小的 | 黑色的 |
產品表
| 產品索引 | 產品編號 | 姓名 |
|---|---|---|
| 1 | 1 | 阿迪達斯 |
| 2 | 2 | 耐克 |
| 3 | 3 | 新百倫 |
因此,顯示的專案應該是 productId 3,2,1
我只需要將所有 productId 數量相加,無論大小和顏色如何。是否按升序從產品表中檢索
這是一個小代碼,只是為了檢索恰好在 orderItems 表中的產品。
SELECT * from Product
inner join OrderItems ON Product.productId = OrderItems.productId where Product.size = OrderItems.size and Product.color = OrderItems.colors
uj5u.com熱心網友回復:
您可以通過 sum() 訂購:
SELECT Product.* from Product inner join OrderItems
ON Product.productId = OrderItems.productId AND
Product.size = OrderItems.size and Product.color = OrderItems.colors
GROUP BY Product.productId
ORDER BY sum(OrderItems.quantity) DESC;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529573.html
標籤:mysql
上一篇:如何在列可以為NULL的情況下使用條件JOINON?
下一篇:Mysql按作業日查詢
