count,sum,avg,max,min__mysql学习笔记

函数

Count

Sum

Max

Min

Avg

 唔,我开始看原版的了

1,count

Used tocount rows or valudes of a column that do not contain a NULL value;

Could beused with the DISTINCT;

All(opposite of DISTINCT) is the default;

Plus:count (*) counts all the rows of a table includingduplicates, whether a NULL value iscontained in a column or not ;

 select Count [(*) | (DISTINCT | ALL)] (COLUMN NAME) from table_name;

Attention:COUNT is used to count rows, not values!!!

 

Count(salary) from table_name;

Count (all salary) from table_name;

Count(*) from table_name;//If we use *,don't write column_name!!!

ANY datatype is OK;

 

2,sum

Returns atotal on the values of a column for a group of rows;

Can beused with distinct,only the distinct rows are totaled;

Select sum([DISTINCT]COLUMN_NAME) FROM TABLE_NAME;

The valudemust be numeric, or there must be somethingwrong;

 

3,avg

SELECT AVG ([DISTINCT]COLUMN_NAME) FROM TABLE_NAME;

Selectavg(salary),avg(money) from table_name;//it's ok;

Attention:insome implementions,the results of your query might be truncatedto the precision of the data type;

 

4,max,min

Return themaximum value from the values of a column in a group of rows;

NULL  values are ignored for both max and min;

Distinctcommand is an option but useless;

Select max([distinct]column_name) from table_name;

MAX andMIN can also be used on character datad.Most commonly your database collationis set to a dictionary order ,so the results arerankedaccording to that.

 

Attention:whenusing aggregate funcitons with the DISTINCTcommand ,our query might not return the desired result;

 

Wecan use them in this way:

Selectcount(C_1),sum(C_2),sum(C_3)/count (C_4) c_4

Fromtable_name;//c_4 is a column alias for the computation.

Itshows:

Count(C_1)   sum(C_2)   c_4

XXX                       XXX        XXXx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值