目录
一:聚合函数
1:count
(列名)返回某一列或者行的总数。某一列有null不参与计算。
Select count(*)|count(列名) from tablename
[WHERE where_definition]
查询student中一共有多少条数据
select count(*) from student;
2:SUM
Sum函数返回满足where条件的行的和
Select sum(列名){,sum(列名)…} from tablename
[WHERE where_definition]
注意:sum仅对数值起作用,否则会报错。求和为字符串的会当作0处理数值的字符会被当作数字处理
求student中年龄总和(其中as 是取别名的意思)
select sum(age) as sum from student;
3:AVG
AVG函数返回满足where条件的一列的平均值
Select sum(列名){,sum(列名)…} from tablename
[WHERE where_definition]
求student中平均年龄
select avg(age)as avg from student;
4:Max/min
Max/min函数返回满足where条件的一列的最大/最小值
Select max(列名) from tablename
[WHERE where_definition]
求studnet中年龄的最大值和最小值
select max(age) as max,min(age) as min from student;
二:字符串函数-concat
Select concat(列名)+ ! from tablename
[WHERE where_definition]
中间连接可以是字符串或者字符
一种是可以拼接多个字符可以每次都要手动添加分割符,一个只需要添加一次
select * ,concat( 字段1.......) from 表名 //从指定表名中显示所有数据和拼接的数据,可以中间加分割数据也会加,要每个都要。
select * concat_ws(分割符,字段.....) from 表名;//从指定表名中显示所有数据和拼接的数据,中间指定分割符而且只要指定一次
将student中的stu_id与name连接用,分割
select concat(stu_id,',',name) as stu from student;
select concat_ws(',',stu_id,name) as stu from student;
三:数值计算函数
round() 取小数点位数
floor() 向下取整
rand() 随机