一,case when 格式
1、 简单Case函数格式 :
case 列名
when 条件值1 then 值1
when 条件值2 then 值2
......
else 默认值 end
简单栗子:
SELECT Id, address, tel,
case name
when 0 then ‘李华’
when 1 then ‘张三’
else '不认识' end
from sqltab where ...
2,Case搜索函数格式 :
case
when 字段 = 条件值1 then 值1
when 字段 = 条件值2 then 值2
else 默认值
end as 列名
再举个栗子:
select ID, name, case when score<=59 then '不及格'
when (score>=60 and score<=79 ) then '良好'
when (score>=80 and score<=100 ) then '优秀'
else '不存在' end as score, age
from sqltab where ...
二、sql函数
1、AVG()
函数返回数值列的平均值
语法: SELECT AVG(column_name) FROM table;
2、COUNT()
函数返回匹配指定条件的行数
用法1: COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入)
SELECT COUNT(column_name) from table;
用法2: COUNT() 函数返回表中的记录数
SELECT COUNT() from table;
注:后面也可以加上where 条件去过滤
3、SUM()
函数返回数值列的总数
语法: SELECT SUM(column_name) from table;
4、MAX()
函数返回指定列的最大值
语法: SELECT MAX(column_name) from table;
5、MIN()
函数返回指定列的最小值
语法: SELECT MIN(column_name) from table;
最后
以上就是呆萌往事最近收集整理的关于sql case when多条件写法及sql常用函数的全部内容,更多相关sql内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复