mysql基础知识

tanyhuan 2020-01-05

一、String 类
1. mysql 常用函数
length, concat, substr, upper, lower, trim,ifnull,isnull
2. 其他函数
(1) lpad, 左边用 \* 号填充, 总字符串为长度为 10;
rpad 右填充
select lpad("小猫", 10, " \* ")
(2)relpace,替换; #
二、数学函数
1.round:四舍五入
2.ceil:向下取整数
3.floor:向上取整数
三、日期函数
1.curdate(),now();
2.将字符串通过指定格式转化成日期:str_to_date
select str_to_date(‘1992-7-12‘,‘%Y-%c-%d‘);
3.将日期转换为字符串:date_format
date_format(‘2019/10/10‘,"%Y-%m-%d")
4.相差多少天
select datediff(max(hiredate),min(hiredate)) from emp;
四、流程控制
1.if语句
select if(10<6,‘true‘,‘false‘);
select price,if(price is null,‘happy‘,‘促销‘,‘sad‘,‘正常价格‘)
2.case语句
case 要判断的字段或表达式
when 常量1 then 要显示的结果1
when 常量2 then 要显示的结果2
else 要显示的结果
end 别名;
五、分组函数
1.group by,sum,max,avg,min,count:分组函数都忽略null值
2.select distinct name,id from user;//distinct必须写在前面
3.select count(distinct name,age)from user;
4.group by:
select column,column2,column3
where condition
group by exxpress_ssion
order by column desc/asc

相关推荐