SQL基础函数

1、avg函数 返回数值列的平均值,注意null值不包括在计算内

--计算学生平均年龄
select avg(age) as age_avg from class
--选取大于平均年龄的学生
select name from class
where age>(select avg(age) from class)  

2、count函数 返回匹配指定条件的行数
select count(age) from class – null值不计入
count(*)函数 返回表中的记录数
count distinct 返回表中不同值的数目

-- 计算李明的所有分数
select count(score) as total_score 
from achevement
where name='LiMing'
-- 计算学生总数
select count(name) as total_num from  class
-- 计算学科总数
select count(distinct subject) as total_subject from achevement

3、frist() 函数 /last()函数 返回字段中记录的第一个/最后一个值

--查找第一个有语文成绩的学生的成绩
select frist(Chinese_score) as first_Chinese from achievement
-- 可使用order by 进行排序

4、max() 函数/min()函数 返回一列中最大值/最小值

select max(Chinese_score) as max_Chinese from achievement
select min(Chinese_score) as max_Chinese from achievement

5、sum()函数 返回数值列的总数

select sum(score) as total_score from achievement

6、group by 语句

select student_id,name,score 
from achievement
group by student_id,score

7、having 子句
– 因where 关键字无法与合计函数一起使用

--查找语数英总分少于180的学生
select student_id,name,total_score 
from achievement
group by student_id
having total_score <180

8、ucase()/lcase()函数 把字段的值转换为大写

select ucase(name) as New_name from class
select ucase(name) as New_name from class

9、mid()函数 用于从文本中提取字符

select mid(city,1,3) as sx_city from class
-- 从第一位开始保留到三位,例如beijing 返回结果为bei

10、len() 函数 返回文本字段中值的长度

select len(city) as len_city from class 
-- 例如beijing返回结果为7

11、round()函数 用于把数值字段舍入为指定的小数位数

select round(height,0) as int_height from class
-- 把学生身高取整

12、now()函数 返回当前的日期和时间

select productname, price,now() as present_date from  

13、format()函数 用于对字段的显示进行格式化

select  student_id,name,format(now(),'YYYY-MM-DD') as present_date
from class

14、date函数

  • now() 返回当前的日期和时间
  • curdate() 返回当前的日期
  • curtime()返回当前的时间
  • date()提取日期或时间的日期部分
  • extract()返回日期/时间的单独部分
  • date_add() 给日期添加指定的时间间隔
  • date_sub() 从日期减去指定的时间间隔
  • datediff() 返回两个日期之间的天数
  • date_format() 用不同的格式显示日期/时间
    date 类型
    • date() YYYY-MM-DD
    • datetime() YYYYY-MM-DD HH:MM:SS
    • timestamp() YYYYY-MM-DD HH:MM:SS
    • time() HH:MM:SS
    • year() 2/4位年格式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值