我有两个表:发票和订单列表
发票包含
+----------------+---------------+-------------+------------+
| orderInvoice | orderCustomer | orderTime | orderTotal |
+----------------+---------------+-------------+------------+
| 00001 | Nick | 2012-11-29 | 30.00 |
| 00002 | Andrew | 2012-11-29 | 15.00 |
| 00003 | West | 2012-11-29 | 80.00 |
+----------------+---------------+-------------+------------+
订单列表包含
+----------------+------------------+------+--------+
| orderInvoice | item | size | Qty | price |
+----------------+------------------+------+--------+
| 00001 | Coke | Can | 20 | 0.50 |
| 00001 | Coke | Bottle | 10 | 2.00 |
| 00002 | Sprite | Can | 30 | 0.50 |
| 00003 | Coke | Bottle | 40 | 2.00 |
+----------------+------------------+------+--------+
我想这样输出:
+---------------------------------------------------+
| Most Popular Soda |
+---------------------------------------------------+
| Coke Bottle 50 |
| Sprite Can 30 |
| Coke Can 20 |
+---------------------------------------------------+
| 2012-11-29 |
+---------------------------------------------------+
我尝试使用 concat() 作为 ITEM 查询它,然后使用 count(ITEM) 。看起来我可以在一个查询中一起使用它们。
------------更新----------------
谢谢你们发布答案。我找到了答案(没有你们的帮助就无法做到)。我会将答案留在那里,以防其他人也寻找它
SELECT
CONCAT(
orderlist.item,
orderlist.size
) AS item,
orderlist.orderInvoice,
SUM(orderlist.Qty) AS Qty
FROM
orderlist
INNER JOIN invoice ON nvoice.orderInvoice = orderlist.orderInvoice
WHERE
Invoice.orderTime = '2012-11-29'
GROUP BY
item
ORDER BY
Qty DESC
最佳答案
试试这个
从订单列表中按项目分组选择项目,总和(数量)作为数量;
关于mysql - 如何从 2 个表中获取最多的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200175/