标签:comm 首字母 用户 img rdate 大于 提高 when 类型
length函数获取参数值的字节个书
select length(‘张三丰hahahah‘);
concat拼接
select concat(last_name,‘_‘,first_name) 姓名 from employees;
substr截取子串
# 截取末尾
select substr(‘李莫愁爱上了路展元‘,6) out_put;
路展元
# 从中间截取
select substr(‘李莫愁爱上了路展元‘,1,3) out_put;
李莫愁
# 姓名中首字母大写,其他字符小写,然后用_拼接,显示出来
select concat(upper(substr(last_name,1,1)),‘_‘,lower(substr(last_name,2))) 姓名 from employees;
# 返回字符串的起始索引,如果找不到返回0
select instr(‘杨不悔爱上殷六侠‘,‘殷六侠‘) as out_put;
殷六侠
upper转换成大写
select lower(‘john‘)
lower转换成小写
select lower(‘JOHN‘)
#将姓变成大写,名变成小写,然后拼接
select concat(upper(first_name),‘_‘,lower(first_name)) 姓名 from employees;
trim去前后指定的空格和字符
select trim(‘ 张翠山 ‘) as out_put;
张翠山
# 去除字符
select trim(‘a‘ from ‘aaaaaa张aaaaaaa翠aaaaaaa山aaaaaaa ‘) as out_put;
张aaaaaaa翠aaaaaaa山aaaaaaa
select replace(‘张无忌爱上了周芷若‘,‘周芷若‘,‘赵敏‘) as out_put;
张无忌爱上了赵敏
select lpad(‘殷素素‘, 10,‘10‘) as out_put;
1010101殷素素
# round 四舍五入
select round(-1.45);
-1
select round(-1.65);
2
select round(-1.6545,2);
-1.65
# rand 随机数
# floor向下取整
select floor(-1.52);
-2
# ceil向上取整,返回大于等于该参数的最小整数
select ceil(1.52);
2
# mod取余
select mod(10,3);
1
# truncate截断: 取小数点后的位数
select truncate(1.6999999,1);
1.6
# now当前系统日期+时间
select now();
2021-05-13 17:59:16
# curdate当前系统日期
select curdate();
# curtime当前系统时间
select curtime();
# 获取指定的部份,年year、月month、日day、小时hour、分钟minute、秒second
select year(now());
# 以英文格式返回月份
select monthname(now()); # 出现英文的月份
may
select month(now());
5
# 返回两个日期相差的天数
select datediff
# str_to_date 将各种格式的日期字符,转换成标准日期表示
# 注意后面的格式是用来解析前面的日期的,并不是最终的输出格式
select str_to_date(‘9.13.2021‘,‘%m.%d.%Y‘);
2021-09-13
# date_format将日期转换成字符,转换要求需要的表示格式
select date_format(now(),‘%y年%m月%d日‘);
21年05月13日
select date_format(now(),‘%y年%c月%d日‘);
21年5月13日
select date_format(now(),‘%Y年%m月%d日‘);
2021年05月13日
select date_format(now(),‘%m月/%d日 %y年‘);
05月/13日 21年
# 查询入职日期为1992-4-3的员工信息
select * from employees where hiredate = ‘1992-04-03‘;
# 查询有奖金的员工名和入职日期(xx月/xx日 xx年)
select last_name, date_format(hiredate,‘%m月/%d日 %y年‘) 入职日期
from employees
where commission_pct is not null;
select if(10>5,‘大‘,‘小‘)
select last_name, commission_pct, if(commission_pct is null,‘没奖金‘,‘有奖金‘) 备注
from employees;
# 代码结构
select 下面要用到的字段或表达式
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
...
else 要显示的值n或语句n;
end
# 案例代码
select salary 原始工资,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资
from employees
order by department_id;
# 代码结构
select 下面要用到的字段或表达式
case
when 条件1 then 要显示的值1或语句1;
when 条件2 then 要显示的值2或语句2;
...
else 要显示的值n或语句n;
end
# 案例代码
select salary,
case
when salary > 20000 then ‘A‘
when salary > 15000 then ‘B‘
when salary > 10000 then ‘C‘
else ‘D‘
end as 工资级别
from employees
select version()查看当前version版本select database() 查看database当前库select user()查看user当前连接用户select password(‘字符‘)返回该字符的密码形式select md5(‘字符‘)返回该字符的md5加密形式sum 求和、max 最大值、min 最小值、avg 平均值、count 计数(group by 后面的那个变量的个数)
select sum(salary) from employees;
select max(salary) from employees;
select min(salary) from employees;
select avg(salary) from employees;
select count(salary) from employees;
select sum(salary) 和, max(salary) 最高, round(avg(salary),2) 平均
from employees
select sum(distinct,salary) from employees;#去重之后的和
select count(salsay) from employees;
select count(*) from employees; # 用来统计行数
select count(1) from employees;
select count(‘字段‘) from employees;
和分组函数一同查询的字段有限制
select avg(salary) department_id from employees;
标签:comm 首字母 用户 img rdate 大于 提高 when 类型
原文地址:https://www.cnblogs.com/qifanren/p/14814449.html