count使用
mysql> select * from test8; +------+------+ | id | name | +------+------+ | 1 | lisi | | 2 | NULL | +------+------+ mysql> select count(*) from test8; +----------+ | count(*) | +----------+ | 2 | +----------+ mysql> select count(name) from test8; +-------------+ | count(name) | +-------------+ | 1 | +-------------+
查询类型为4的库存
select sum(goods_number) from goods where cat_id=4;
group by
统计一下每个类型分组下的库存
mysql> select cat_id,sum(goods_number) from goods group by cat_id; +--------+-------------------+ | cat_id | sum(goods_number) | +--------+-------------------+ | 2 | 0 | | 3 | 203 | | 4 | 4 | | 5 | 8 | | 8 | 61 | | 11 | 23 | | 13 | 4 | | 14 | 9 | | 15 | 2 | +--------+-------------------+
不是标准的sql语句,逻辑上解释不通(每个类别cat_id里有很多goods_name)
不推荐 select goods_name ,sum(goods_number) from goods group by cat_id;
解释:在select a/b中必须在group by a/b/c语意上才没有问题
技巧:查询语句理解上从后面的条件过滤开始,先理解过滤条件,再看前面的执行