订单表
顾客ID | 产品编号 | 数量 | 价钱 |
---|---|---|---|
1 | 2 | 5 | 100 |
1 | 3 | 2 | 200 |
1 | 4 | 1 | 500 |
2 | 1 | 4 | 50 |
3 | 5 | 6 | 700 |
按特定列分组时,仅返回此列的唯一值。
SELECT customerId FROM orders GROUP BY customerId;
返回值:
顾客ID |
---|
1 |
2 |
3 |
聚合函数count()适用于每个组,而不适用于完整的表:
SELECT customerId, COUNT(productId) as numberOfProducts, sum(price) as totalPrice FROM orders GROUP BY customerId;
返回值:
顾客ID | 产品数量 | 总价 |
---|---|---|
1 | 3 | 800 |
2 | 1 | 50 |
3 | 1 | 700 |