标签:switch 有奖 级别 dep date() 表达式 center HERE use
功能:类似于java中的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:
1、隐藏了实现细节
2、提高代码的重用性
调用:
select 函数名(实参列表)【 from 表】;
特点:
①函数名
②函数功能
分类:
1、单行函数
如:CONCAT、LENGTH、IFNULL等
字符函数、数学函数、日期函数、其它函数、流程控制函数
2、分组函数
功能:做统计使用,又称为统计函数、聚合函数、组函数
函数总结
字符函数 | 数学函数 | 日期函数 | 其它函数 | 流程控制函数 |
---|---|---|---|---|
LENGTH | ROUND | NOW | VERSION | IF |
CONCAT | CEIL | CURDATE | DATABASE | CASE |
UPPER | FLOOR | CURTIME | USER | |
LOWER | TRUNCATE | YEAR MONTH DAY | ||
SUBSTR SUBSTRING | MOD | HOUR MINUTE SECOND | ||
INSTR | STR_TO_DATE | |||
TRIM | DATE_FORMAT | |||
LPAD RPAD | ||||
REPLACE |
SELECT LENGTH(‘Fenix‘);
SELECT LENGTH(‘高飞‘); # UTF-8 一个汉字占3个字节
SHOW VARIABLES LIKE ‘%char%‘;
SELECT CONCAT(last_name,‘___‘,first_name) AS 姓名
FROM employees;
SELECT UPPER(‘john‘);
SELECT LOWER(‘FENIX‘);
SELECT
CONCAT(UPPER(first_name),‘___‘,LOWER(last_name)) AS 姓名
FROM
employees;
# 截取从指定索引处后面的所有字符
# 输出789
SELECT SUBSTR(‘123456789‘,7) AS out_put; # 索引从1开始
# 截取从指定索引处指定字符长度的字符
# 输出345
SELECT SUBSTR(‘123456789‘,3,3) AS out_put;
SELECT
CONCAT(UPPER(SUBSTR(last_name,1,1)),‘_‘,LOWER(SUBSTR(last_name,2)))
FROM
employees;
# 用于返回子串在起始串中第一次出现的索引,如果找不到返回0
SELECT
INSTR(123456789,567);
SELECT
INSTR(123456789,457);
#去掉前后空格
SELECT TRIM(‘ 12345 ‘);
SELECT LENGTH(‘ 12345 ‘); #length = 9
SELECT LENGTH(TRIM(‘ 12345 ‘)); #length = 5
SELECT TRIM(‘aa‘ FROM ‘aaaaaa12aa3aa45aaa‘); # 在前后两端中去除子串
SELECT LPAD(‘12345‘,10,‘*‘) AS out_put; #*****12345
SELECT LPAD(‘12345‘,3,‘-‘) AS out_put ; # 123
#用指定的字符实现右填充指定长度
SELECT RPAD(12345,10,‘aa‘)AS out_put; #12345aaaaa
SELECT RPAD(12345,3,‘--‘) AS OUT_PUT; #123
SELECT REPLACE(‘张无忌爱上了周芷若1,张无忌爱上了周芷若2‘,‘周芷若‘,‘赵敏‘) AS out_put;
SELECT ROUND(1.65) #2
SELECT ROUND(-1.3) #1
SELECT ROUND(1.5678,2) # 1.57 # 保留2位
SELECT CEIL(0.001) ;# 1
SELECT CEIL(1.00); # 1
SELECT FLOOR(-9.99); # -10
SELECT FLOOR(9.99) ;# 9
SELECT TRUNCATE(1.666666,3) # 1.666
SELECT MOD(10,3); #1
SELECT 10%3;#1
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT YEAR(NOW()) AS 年; #2020
SELECT YEAR(‘1996-09-09‘) AS 出生年; #1996
SELECT SECOND(NOW()) AS 秒; #1~60
SELECT MONTH(‘1996-09-09‘); #9
SELECT MONTHNAME(NOW()); #获取当前月份对应的英文---June
SELECT STR_TO_DATE(‘1996-9-9‘,‘%Y-%c-%d‘) AS out_put;
# 非格式化
SELECT *
FROM employees
WHERE hiredate = ‘1992-4-3‘;
# 格式化
SELECT *
FROM employees
WHERE hiredate = STR_TO_DATE(‘4-3 1992‘,‘%c-%d %Y‘);
SELECT DATE_FORMAT(NOW(),‘%y年%m月%d日‘) AS out_put;
SELECT last_name,DATE_FORMAT(hiredate,‘%m月/%d日 %y年‘) AS 入职日期
FROM employees
WHERE commission_pct IS NOT NULL;
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
SELECT IF(10>5,‘大‘,‘小‘) AS out_put;
SELECT last_name, commission_pct ,
IF(commission_pct IS NULL,‘没奖金‘,‘有奖金‘) AS 奖金情况
FROM employees;
使用一: switch case的效果
Java:
(擅长处理等值判断)
switch(变量或表达式){
case 常量1: 语句1; break;
...
default:语句n;break;
}MySQL:
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2
···
else 要显示的值n或语句n;
end
/*
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
其他部门,显示的工资为原始工资
*/
# 注意 select 语句和case语句之间要加‘,‘ 否则会报1064的语法错误
SELECT department_id AS 部门号, salary AS 原始工资,
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;
使用二:类似于多重if
Java:
(区间判断)
if(条件1){
语句1;
}
else if(条件2){
语句2;
}
···
else{
语句n;
}MySQL:
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2
···
else 要显示的值n或语句n
/*
如果工资>20000,显示A级别
如果工资>15000,显示B级别
如果工资>10000,显示C级别
否则,显示D级别
*/
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 CONCAT(last_name,‘ earns ‘,salary,‘ monthly but wants ‘,salary*3) AS OUT_PUT
FROM employees
WHERE last_name LIKE ‘K%‘ AND salary = 24000;
SELECT last_name, job_id AS job,
CASE job_id
WHEN ‘AD_PRES‘ THEN ‘A‘
WHEN ‘ST_MAN‘ THEN ‘B‘
WHEN ‘IT_PROG‘ THEN ‘C‘
WHEN ‘SA-REP‘ THEN ‘D‘
WHEN ‘ST-CLERK‘ THEN ‘E‘
END AS grade
FROM employees
WHERE job_id = ‘AD_PRES‘;
标签:switch 有奖 级别 dep date() 表达式 center HERE use
原文地址:https://www.cnblogs.com/fenixG/p/13024439.html