-1

我有一张存储厨房库存的桌子。:

表结构: tbl_inv

╔══════════════╦══════════╦══════╗
║ MATERIALNAME ║ QUANTITY ║ UNIT ║
╠══════════════╬══════════╬══════╣
║ chicken      ║        5 ║ kgs  ║
║ cheese       ║       10 ║ kgs  ║
║ chicken      ║       10 ║ pcs  ║
║ cheese       ║       10 ║ kgs  ║
║ chicken      ║       10 ║ kgs  ║
╚══════════════╩══════════╩══════╝

我想在我的 html 页面上输出这个 -

╔═════════════╦══════════╦══════╗
║MATERIALNAME ║ TOTALSUM ║ UNIT ║
╠═════════════╬══════════╬══════╣
║ cheese      ║       20 ║ kgs  ║
║ chicken     ║       15 ║ kgs  ║
║ chicken     ║       10 ║ pcs  ║
╚═════════════╩══════════╩══════╝

我尝试过遵循 SQL,

SELECT materialname, SUM(quantity) totalValue FROM tbl_inv GROUP BY materialname";

我不知道我应该在 mysql 命令中使用什么来获得这个输出。

4

2 回答 2

2

materialName你基本上将它们分组unit

SELECT  materialName, SUM(quantity) totalSum, unit
FROM    tableName
GROUP   BY materialName, unit

其他)

于 2013-01-26T10:31:54.233 回答
1

用排序查询试试这个 Group by...

SELECT  materialName MATERIALNAME, SUM(quantity) TOTALSUM, unit UNIT
FROM    tableName
GROUP   BY MATERIALNAME, UNIT
ORDER   BY MATERIALNAME 
于 2013-01-26T10:51:41.187 回答