我有一个包含3列的表格,如下所示:
+------------+---------------+-------+
| id | category | price |
+------------+---------------+-------+
1 | Home | 20
2 | Transport | 30
3 | General | 40
4 | General | 50
我想要的结果是这样的:
category price
+------------+---------------+-------+
Home 20
Transport 30
General 90
我需要没有任何重复的类别名称。其实我需要他们的id和类别,这个最好的SQL命令是什么?我在下面的表格中使用了DISTINCT但是我无法获得适当的结果。
SELECT
MIN(id) AS id, `categories`, `expenses_price`
FROM
expensess
WHERE
SUM(expenses_price)
GROUP BY `categories`
但总计不计算
答案 0 :(得分:1)
尝试此操作以获取最小ID和价格总和:
SELECT
min(id) as ID, category, SUM(price)
FROM
expenses
GROUP BY category
ORDER by ID
答案 1 :(得分:0)
这很简单group by
SELECT
min(id) id, `categories`, SUM(price) `expenses_price`
FROM
expensess
GROUP BY `categories`