Basics mysql learning 05

mysql statistical functions:

1. check prices highest price:

select max(shop_price) from goods;

 

2. Query commodity prices, the lowest price:

select min(shop_price) from goods;

 

3. The total inventory requirements:

select sum(goods_number) from goods;

 

 4. Check the average price of all commodities:

select avg(shop_price) from goods;

 

There are many kinds of goods within 5. Statistical Mall (not a number):

select count(*) from goods;

count () function is a statistical line number, reason to use count (*) instead of count (column name) because if there is a behavior that column empty, then count (column name) does not take it into account.

Look at a small example:

Even if there is a row full of empty, with the count (*) It can also be taken into account:

 

 Statistical functions used alone has little significance, and to have the group meet up with:

mysql the group by grouping queries:

Calculate the amount of inventory of all items at the third and columns :

select cat_id,sum(goods_number) from goods  where cat_id=3 group by cat_id;

 

 一次计算完每一个栏目下的库存量之和:

select cat_id,sum(goods_number) from goods  group by cat_id;

严格的讲,以group by a,b,c为列,select的列只能在a,b,c或者是统计函数中选择,这样在语义上才没有矛盾。

 做个小练习:有以下成绩表,求出挂科数等于或高于2门的学生的平均成绩:

 

select name,avg(score),sum(score<60)as fail from test5 group by name having fail >=2;

 

排序:order by

取出第四个栏目下的商品,把价格由高到低进行排序:

select *from goods where cat_id=4 order by shop_price desc; #desc 代表降序排列

 

按栏目升序排列,同一个栏目下的商品按价格降序排列

select * from goods order by cat_id asc,shop_price desc;

 

 限制输出条目limit:

取出商品价格前十的商品:

select goods_id,goods_name,cat_id,shop_price from goods order by shop_price desc limit 10;

最后强调一点:Where,group by,having,order by,limit 五个子句顺序不能乱!

 

Guess you like

Origin www.cnblogs.com/wanghaoyu666/p/11274575.html