我有两张表:发票和订单
发票包含
+----------------+---------------+-------------+------------+
| 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进行查询,然后计数(ITEM)。看起来我可以在一个查询中一起使用它们。
------------ ---------------- UPDATE
谢谢你们发布答案。我想出了答案(如果没有你的帮助,就无法做到)。我将在那里留下答案,以防其他人也在寻找它
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
答案 0 :(得分:2)
试试这个
select item,sum(qty) as quantity from orderlist group by item;
答案 1 :(得分:0)
试试这个:
Select item, size, SUM(qty) quantity
from
orderlist
group by item, size
order by quantity desc