描述
OrderItems表代表售出的产品,quantity代表售出商品数量。
quantity |
10 |
100 |
1000 |
10001 |
2 |
15 |
【问题】编写 SQL 语句,确定已售出产品的总数。
【示例结果】返回items_ordered列名,表示已售出商品的总数。
items_ordered |
11128 |
示例1
输入:
DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
quantity INT(16) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES (10),(100),(1000),(10001),(2),(15);
复制
输出:
items_ordered
11128.000
答案
sum(字段名),求和
select sum(quantity) as items_ordered from OrderItems
sum函数的其他用法
sum(if(result='true',1,0)) --如果result='true',就+1,否则+0
sum(case when result = 'true' then 1 else 0 end) --跟上面一个含义