MySQL查询一个简单的表

时间:2013-11-29 01:39:39

标签: mysql sql

我的表:

+----+---------+----------+--------+--------------+
| id | size    | quantity | status | customers_id |
+----+---------+----------+--------+--------------+
| 12 | 10_feet | 3        | active |            0 |
| 13 | 10_feet | 3        | active |            0 |
| 14 | 10_feet | 2        | active |            0 |
| 15 | 14_feet | 5        | active |            0 |
| 16 | 14_feet | 2        | active |            0 |
| 17 | 14_feet | 2        | active |            0 |
| 18 | 17_feet | 2        | active |            0 |
| 19 | 17_feet | 2        | active |            0 |
| 20 | 24_feet | 3        | active |            0 |
+----+---------+----------+--------+--------------+

寻找查询以显示每个尺寸和数量的总和。

10_feet  8   active
14_feet  9   active
17_feet  4   active
24_feet  3   active

我正在使用的查询是

SELECT *, SUM(quantity)  FROM`trucks` GROUP BY size;

但它只显示

10_feet   3 active
14_feet   5 active
17_feet   2 active
24_feet   3 active

1 个答案:

答案 0 :(得分:0)

试试这个:

select a.size,sum(a.qty),a.status from table a inner join table b on a.id=b.id group by a.size