
使用一条SQL语句,对产品的库存查询。
产品的表结构
CREATE TABLE `temp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`flag` int(1) DEFAULT NULL,
`size` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
| 字段 | 描述 |
|---|---|
| id | 主键 |
| product_id | 产品唯一标识 |
| flag | 标记位,1代表入库;2代表出入 |
| size | 数量 |
使用一条sql语句查询每个产品的库存状态
select sum(
case flag
when 1 then size
when -1 then -1*size
end)
from temp
group by product_id
主要实现采用when .. then




近期评论