mysql常用函数
1.concat(str1,str2...)字段拼接
mysql中‘+’号只能做数学运算,字符串的拼接可以使用concat(str1,str2)函数做拼接
SELECT CONCAT(`first_name`,`last_name`) FROM employees;
2.distinct 关键字去重
# 获得的job_id字段去重
SELECT DISTINCT `job_id` FROM employees;
3.ifnull(str,想转的值)
# 意思就是如果commission_pct字段为null,就转为0
SELECT IFNULL(commission_pct,0),`commission_pct` FROM employees;
4.length(str) 获取str的字节数
5.upper(str)转大写,lower(str)转小写
6.substr(string,索引)
mysql中索引都是从1开始的
substr(string,2)-->截取从2开始的所有,tring
substr(string,1,3)-->1是索引开始,3是截取的位数,str
7.instr(string,t)-->子串第一次出现的索引,找不到则返回0
8.trim(str) -->去字符串前后的空格
9.lpad('str',10,'#') --->用指定的字符#,实现左填充指定的长度
10.rpad右填充
11.replace('string','a','b') --->把所有a替换成b
12.now()系统日期加时间,curdate()系统日期不加时间,curtime()系统时间,不包含日期
13.if(条件,值1,值2) -->类似于三目运算符,条件为真执行值1,为假则值2;
14.case
- 1.第一种,(类似switch ,要等值判断)
case 要判断的字段或者表达式 when 常量1 then 显示的值1或者语句;when 常量2 then 显示的值2或者语句 。。。else 要显示的值或者语句 end - 2.第二种
case when 判断条件 then 显示的值1或者语句; when 条件 then 值2或者语句 else 值3 end
When nothing seems to help, I go look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before. -- Jacob Riis