情况:
有两个表Head
和Item
,其中每个 Head 与一个或多个 Item 相关。我想显示每个项目、每个月的每个项目数量的总和,以及所有大于特定值的数量的列表。
头:
| OrderID | Timestamp | <several other columns> |
-------------------------------------------------
| 1 | 6548972 | ... |
| 10 | 6548978 | ... |
| ... | ... | ... |
-------------------------------------------------
物品:
| ItemId | OrderID | Quantity | <several other columns> |
---------------------------------------------------------
| 21 | 1 | 5 | ... |
| 22 | 1 | 3 | ... |
| 25 | 10 | 1 | ... |
| ... | ... | ... | ... |
---------------------------------------------------------
询问:
SELECT
SUM(Item.Quantity) AS `total`,
GROUP_CONCAT(Item.Quantity ORDER BY Item.Quantity DESC SEPARATOR ', ') AS `quantities`,
FROM_UNIXTIME(Head.Timestamp, '%m') AS `month`,
COUNT(Head.OrderID) AS `orders`,
Item.ItemID,
FROM
Item LEFT JOIN (Head) ON (Head.OrderID = Item.OrderID)
WHERE
(Head.Timestamp BETWEEN <sometime> AND <someothertime>)
GROUP BY
`month`,
Item.ItemID
ORDER BY
`total` ASC
我的工作:
鉴于上述情况和查询,我能够实现我的目标,除了 GROUP_CONCAT 提供了所有数量的列表,这是不受欢迎的。相反,我希望显示的所有数量都更大,比如说4
当前结果:
| total | quantities | month | orders | ItemID |
------------------------------------------------
| 8 | 5, 2, 1 | 04 | 3 | 21 |
| 3 | 3 | 04 | 1 | 22 |
| 20 | 10, 9, 1 | 04 | 3 | 25 |
期望的结果:
| total | quantitiesGreater4 | month | orders | ItemID |
--------------------------------------------------------
| 8 | 5 | 04 | 3 | 21 |
| 3 | | 04 | 1 | 22 |
| 20 | 10, 9 | 04 | 3 | 25 |
最后一个问题:
有没有办法修改原始查询以显示所需的结果?或者我的 php 脚本会更好地执行这种工作吗?
更新: 我不希望数量 <= 4 被从总数中过滤掉,我只是不希望它们出现在数量列表中