DDL:数据定义语句[create表,库…] DML:数据操作语句[增加insert,修改update,删除 delete] DQL:数据查询语句[select ] DCL:数据控制语句[管理数据库:比如用户权限grant revoke ]
mysql中的+号: select ‘john’+90; 如果转换失败,则将字符型数值转换成0 select null+10; 只要其中一方为null,则结果肯定为null 与null进行+时,使用ifnull( xxx , 0 ) ifnull(commission_pct,0) 判断是否为0
条件查询 语法: select 查询列表 from 表名 where 筛选条件; 分类: 一、按条件表达式筛选 简单条件运算符:> < = != <> >= <= <> 不等于 二、按逻辑表达式筛选 逻辑运算符: 作用:用于连接条件表达式 && || ! and or not
&&和and:两个条件都为true,结果为true,反之为false ||或or: 只要有一个条件为true,结果为true,反之为false !或not: 如果连接的条件本身为false,结果为true,反之为false
三、模糊查询 like between and in is null
create database 数据库名;
drop database 数据库名;
create database test character set utf8
create database test character set utf8 collate utf8_bin
show databases;
show create database `test`
drop database test
创建表
CREATE TABLE table_name(
field1 datatype,field2 datatype,field3 datatype
)character set字符集collate校对规则engine存储引擎field:指定列名datatype:指定列类型(字段类型)
character set:如不指定则为所在数据库字符集collate:如不指定则为所在数据库校对规则
engine:引擎
修改表
查看表的结构:desc表名;
修改表字符集:alter table表名character set字符集;
员工表 emp 的上增加一个 image 列,varchar 类型(要求在 resume 后面)。
ALTER TABLE emp
ADD image VARCHAR(32) NOT NULL DEFAULT '' AFTER RESUME
修改 job 列,使其长度为 60。
ALTER TABLE emp
MODIFY job VARCHAR(60) NOT NULL DEFAULT ''
删除 sex 列。
ALTER TABLE emp DROP sex
表名改为 employee。
RENAME TABLE emp TO employee
修改表的字符集为 utf8
ALTER TABLE employee CHARACTER SET utf8
列名 name 修改为 user_name
ALTER TABLE employee
CHANGE `name` `user_name` VARCHAR(64) NOT NULL DEFAULT ''
DESC employee
数据库C[create]R[read]U[update]D[delete]语句
1.Insert语句(添加数据)
2. Update语句(更新数据)
3.Delete语句(删除数据)
4.Select语句(查找数据)
INSERT INTO `goods` (id, goods_name, price) VALUES(20, '苹果手机', 3000);
将姓名为 小王 的员工薪水修改为 3000 元。
UPDATE employee SET salary = 3000
WHERE user_name = '小王'
删除表中名称为’老王’的记录。
DELETE FROM employee WHERE user_name = '老王';
SELECT last_name,salary,email FROM employees;
SELECT * FROM employees;
SELECT DISTINCT department_id FROM employees;
select concat(last_name,first_name) as 姓名 from employees;
select * from employees
where not(department_id>=90 and department_id<=110) or salary>15000;
模糊查询
1.like
特点:
①一般和通配符搭配使用
通配符:
% 任意多个字符,包含0个字符
_ 任意单个字符
查找 " _ " ,使用 '_$_%' ESCAPE '$'
select * from employees
where last_name like '%a%';
select last_name, salary from employees
where last_name LIKE '__e_a%';
select last_name from employees
where last_name like '_$_%' escape '$';
2.between and
①使用between and 可以提高语句的简洁度
②包含临界值
③两个临界值不要调换顺序
select * from employees
where employee_id >= 100 and employee_id<=120;
3.in
含义:判断某字段的值是否属于in列表中的某一项
特点:
①使用in提高语句简洁度
②in列表的值类型必须一致或兼容
③in列表中不支持通配符
select last_name, job_id FROM employees
where job_id in( 'IT_PROT' ,'AD_VP','AD_PRES');
( job_id = 'IT_PROT' OR job_id = 'AD_VP' OR JOB_ID ='AD_PRES'; )
4、is null
=或<>不能用于判断null值
is null或is not null 可以判断null值
select last_name, commission_pct from employees
where commission_pct is null;
(
WHERE commission_pct IS NOT NULL;)
SELECT last_name,commission_pct
FROM employees
WHERE commission_pct <=>NULL;
SELECT last_name, salary
FROM employees
WHERE salary <=> 12000;
排序查询
select 查询列表 from 表名 (where 筛选条件) order by 排序的字段或表达式; 特点: 1、asc代表的是升序,可省略 desc代表的是降序 2、order by子句可以支持 单个字段、别名、表达式、函数、多个字段 3、order by子句在查询语句的最后面,除了limit子句
SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees
WHERE department_id>=90
ORDER BY employee_id DESC;
SELECT *,salary*12*(1+IFNULL(commission_pct,0))
FROM employees
ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 ASC;
SELECT length(last_name),last_name
FROM employees
ORDER BY length(last_name) DESC;
SELECT *
FROM employees
ORDER BY salary DESC,employee_id ASC;
分组函数
功能:用作统计使用,又称为聚合函数或统计函数或组函数
分类:
sum 求和、avg 平均值、max 最大值 、min 最小值 、count 计算个数
特点:
1、sum、avg一般用于处理数值型
max、min、count可以处理任何类型
2、以上分组函数都忽略null值
3、可以和distinct搭配实现去重的运算
4、count函数的单独介绍 一般使用count(*)用作统计行数
5、和分组函数一同查询的字段要求是group by后的字段
1、简单 的使用
SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 个数
FROM employees;
2、和distinct搭配
SELECT COUNT(distinct salary),COUNT(salary) FROM employees;
3、count函数的详细介绍 :统计个数
SELECT COUNT(salary) FROM employees;
SELECT COUNT(*) FROM employees; (和COUNT(1) 差不多)
SELECT COUNT(1) FROM employees;
效率:
MYISAM存储引擎下 ,COUNT(*)的效率高
INNODB存储引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(字段)要高一些
4、和分组函数一同查询的字段有限制
SELECT AVG(salary),employee_id FROM employees;
分组查询
语法:
select 查询列表 from 表
【where 筛选条件】
group by 分组的字段 【order by 排序的字段】;
特点:
1、和分组函数一同查询的字段必须是group by后出现的字段
2、筛选分为两类:分组前筛选和分组后筛选
针对的表 位置 连接的关键字
分组前筛选 原始表 group by前 where
分组后筛选 group by后的结果集 group by后 having
①分组函数做条件肯定是放在having字句中
②能用分组前筛选的,就优先考虑使用分组前筛选
3、group by 子句支撑单个字段分组,多个字段分组(多个字段之间用逗号隔开没有顺序要求),表达式或函数(用得较少)
4、也可以添加排序(排序放在整个分组查询的最后)
问题1:分组函数做筛选能不能放在where后面
答:不能
问题2:where——group by——having
一般来讲,能用分组前筛选的,尽量使用分组前筛选,提高效率
1.简单的分组
SELECT AVG(salary),job_id
FROM employees
GROUP BY job_id;
SELECT COUNT(*) AS 部门个数,location_id
FROM departments
GROUP BY location_id;
2、可以实现分组前的筛选
SELECT MAX(salary),department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;
SELECT AVG(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
3、分组后筛选
SELECT COUNT(*) AS 个数,department_id
FROM employees
GROUP BY department_id;
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;
SELECT job_id,MAX(salary)
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
①
SELECT manager_id,MIN(salary)
FROM employees
GROUP BY manager_id;
②
SELECT manager_id,MIN(salary)
FROM employees
WHERE manager_id>102
GROUP BY manager_id ;
3
SELECT manager_id,MIN(salary)
FROM employees
WHERE manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000 ;
4.添加排序
SELECT job_id,MAX(salary) m
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING m>6000
ORDER BY m ;
5.按多个字段分组
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY job_id,department_id;
SELECT MIN(salary),job_id,department_id
FROM employees
GROUP BY department_id,job_id
ORDER BY MIN(salary) DESC;
连接查询(多表查询)★★★★★
笛卡尔乘积现象:表1 有m行,表2有n行,结果=m*n行 发生原因:没有有效的连接条件 如何避免:添加有效的连接条件 分类: 按年代分类: sql92标准:仅仅支持内连接 sql99标准【推荐】:支持内连接+外连接(左外和右外)+交叉连接 按功能分类: 内连接: 1.等值连接 2.非等值连接 3.自连接 外连接: 1.左外连接 2.右外连接 3.全外连接 4.交叉连接
```sql
语法:
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
【where 筛选条件】
【group by 分组】
【having 筛选条件】
【order by 排序列表】
分类:
内连接(★):inner
外连接
左外(★):left 【outer】
右外(★):right 【outer】
全外:full【outer】
交叉连接:cross
A表
id name
1 小王
2 小李
3 小刘
B表
id A_id job
1 2 老师
2 4 程序员
内连接:(只有2张表匹配的行才能显示)
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件;
分类:等值 非等值 自连接
特点:
①添加排序、分组、筛选
②inner可以省略
③ 筛选条件放在where后面,连接条件放在on后面,提高分离性,便于阅读
④inner join连接和sql92语法中的等值连接效果是一样的,都是查询多表的交集
select a.name,b.job from A a inner join B b on a.id=b.A_id
只能得到一条记录
name job
小李 老师
外连接
应用场景:用于查询一个表中有,另一个表没有的记录
特点:
1、外连接的查询结果为主表中的所有记录
如果从表中有和它匹配的,则显示匹配的值
如果从表中没有和它匹配的,则显示null
外连接查询结果=内连接结果+主表中有而从表没有的记录
2、左外连接,left join左边的是主表
右外连接,right join右边的是主表
3、左外和右外交换两个表的顺序,可以实现同样的效果
4、全外连接=内连接的结果+表1中有但表2没有的+表2中有但表1没有的
左外连接:(左边的表不加限制)
select a.name,b.job from A a left join B b on a.id=b.A_id
三条记录
name job
小王 null
小李 老师
小刘 null
右外连接:(右边的表不加限制)
select a.name,b.job from A a right join B b on a.id=b.A_id
两条记录
name job
小李 老师
null 程序员
全外连接:(左右2张表都不加限制)
select a.name,b.job from A a full join B b on a.id=b.A_id
四条数据
name job
小王 null
小李 老师
小刘 null
null 程序员
交叉连接:cross
SELECT a.*,b.* FROM A a CROSS JOIN B b ON a.id;
结果近似与笛卡尔乘积
连接详细
一)内连接
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件;
分类: 等值 非等值 自连接
特点:
①添加排序、分组、筛选
②inner可以省略
③ 筛选条件放在where后面,连接条件放在on后面,提高分离性,便于阅读
④inner join连接和sql92语法中的等值连接效果是一样的,都是查询多表的交集
1、等值连接
SELECT last_name,department_name
FROM departments d
INNER JOIN employees e
ON e.`department_id` = d.`department_id`;
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`= j.`job_id`
WHERE e.`last_name` LIKE '%e%';
SELECT city,COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
SELECT COUNT(*),department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
SELECT COUNT(*) 个数,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.`department_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;
二)非等值连接
SELECT salary,grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
SELECT COUNT(*),grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`
GROUP BY grade_level
HAVING COUNT(*)>20
ORDER BY grade_level DESC;
三)自连接
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`;
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`
WHERE e.`last_name` LIKE '%k%';
二、外连接
应用场景:用于查询一个表中有,另一个表没有的记录
特点:
1、外连接的查询结果为主表中的所有记录
如果从表中有和它匹配的,则显示匹配的值
如果从表中没有和它匹配的,则显示null
外连接查询结果=内连接结果+主表中有而从表没有的记录
2、左外连接,left join左边的是主表
右外连接,right join右边的是主表
3、左外和右外交换两个表的顺序,可以实现同样的效果
4、全外连接=内连接的结果+表1中有但表2没有的+表2中有但表1没有的
SELECT * FROM beauty;
SELECT * FROM boys;
左外连接
SELECT b.*,bo.*
FROM boys bo
LEFT OUTER JOIN beauty b
ON b.`boyfriend_id` = bo.`id`
WHERE b.`id` IS NULL;
SELECT d.*,e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.`department_id` = e.`department_id`
WHERE e.`employee_id` IS NULL;
右外
SELECT d.*,e.employee_id
FROM employees e
RIGHT OUTER JOIN departments d
ON d.`department_id` = e.`department_id`
WHERE e.`employee_id` IS NULL;
全外
USE girls;
SELECT b.*,bo.*
FROM beauty b
FULL OUTER JOIN boys bo
ON b.`boyfriend_id` = bo.id;
交叉连接
SELECT b.*,bo.*
FROM beauty b
CROSS JOIN boys bo;
子查询
含义:
出现在其他语句中的select语句,称为子查询或内查询
外部的查询语句,称为主查询或外查询
分类:
按子查询出现的位置:
select后面:仅仅支持标量子查询
from后面:支持表子查询
where或having后面:★
标量子查询(单行) √
列子查询 (多行) √
行子查询
exists后面(相关子查询)
表子查询
按结果集的行列数不同:
标量子查询(结果集只有一行一列)
列子查询(结果集只有一列多行)
行子查询(结果集有一行多列)(使用较少,了解)
表子查询(结果集一般为多行多列)
1、标量子查询(单行子查询)
2、列子查询(多行子查询)
返回多行
使用多行比较操作符
操作符 含义
in/not in ★ 等于列表中的任意一个
any|some 和子查询返回的某一个值比较
all 和子查询返回的所有值比较
3、行子查询(多列多行)
特点:
①子查询放在小括号内
②子查询一般放在条件的右侧
③标量子查询,一般搭配着单行操作符使用
> < >= <= = <>
列子查询,一般搭配着多行操作符使用
in、any/some、all
④子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果
1.标量子查询★
案例1:谁的工资比 Abel 高?
①查询Abel的工资
SELECT salary
FROM employees
WHERE last_name = 'Abel'
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
SELECT job_id
FROM employees
WHERE employee_id = 141
SELECT salary
FROM employees
WHERE employee_id = 143
SELECT last_name,job_id,salary
FROM employees
WHERE job_id = (
SELECT job_id
FROM employees
WHERE employee_id = 141
) AND salary>(
SELECT salary
FROM employees
WHERE employee_id = 143
);
SELECT MIN(salary)
FROM employees
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT salary
FROM employees
WHERE department_id = 250
);
2.列子查询(多行子查询)★
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
SELECT last_name
FROM employees
WHERE department_id INL(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN( salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
3、行子查询(结果集一行多列或多行多列)
SELECT *
FROM employees
WHERE (employee_id,salary)=(
SELECT MIN(employee_id),MAX(salary)
FROM employees
);
SELECT MIN(employee_id)
FROM employees
SELECT MAX(salary)
FROM employees
SELECT *
FROM employees
WHERE employee_id=(
SELECT MIN(employee_id)
FROM employees
)AND salary=(
SELECT MAX(salary)
FROM employees
);
二、select后面
仅仅支持标量子查询
SELECT d.*,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.`department_id`
) 个数
FROM departments d;
SELECT (
SELECT department_name
FROM departments d
INNER JOIN employees e
ON d.department_id=e.department_id
WHERE e.employee_id=102
) 部门名;
三、from后面
将子查询结果充当一张表,要求必须起别名
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
SELECT * FROM job_grades;
SELECT ag_dep.*,g.`grade_level`
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;
四、exists后面(相关子查询)
语法:
exists(完整的查询语句)
结果:
1或0
SELECT EXISTS(SELECT employee_id FROM employees WHERE salary=300000);
SELECT department_name
FROM departments d
WHERE d.`department_id` IN(
SELECT department_id
FROM employees
)
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.`department_id`=e.`department_id`
);
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN(
SELECT boyfriend_id
FROM beauty
)
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
SELECT boyfriend_id
FROM beauty b
WHERE bo.`id`=b.`boyfriend_id`
);
分页查询 ★
应用场景:当要显示的数据,一页显示不全,需要分页提交sql请求
语法:
select 查询列表
from 表
【join type join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序的字段】
limit 【offset,】size;
offset要显示条目的起始索引(起始索引从0开始)
size 要显示的条目个数
特点:
①limit语句放在查询语句的最后
②公式
要显示的页数 page,每页的条目数size
select 查询列表
from 表
limit (page-1)*size,size;
size=10
page
1 0
2 10
3 20
SELECT * FROM employees LIMIT 0,5;
SELECT * FROM employees LIMIT 5;
SELECT * FROM employees LIMIT 10,15;
SELECT *
FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC
LIMIT 10 ;
联合查询
union 联合 合并:将多条查询语句的结果合并成一个结果
语法:
查询语句1
union
查询语句2
union
...
应用场景:
要查询的结果来自于多个表,且多个表没有直接的连接关系,但查询的信息一致时
特点:★
1、要求多条查询语句的查询列数是一致的!
2、要求多条查询语句的查询的每一列的类型和顺序最好一致
3、union关键字默认去重,如果使用union all 可以包含重复项
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;;
SELECT * FROM employees WHERE email LIKE '%a%'
UNION
SELECT * FROM employees WHERE department_id>90;
常见函数
概念:类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1、隐藏了实现细节 2、提高代码的重用性
调用:select 函数名(实参列表) 【from 表】;
特点:
①叫什么(函数名)
②干什么(函数功能)
分类:
1、单行函数
如 concat、length、ifnull等
2、分组函数
功能:做统计使用,又称为统计函数、聚合函数、组函数
常见函数:
一、单行函数
字符函数:
length:获取字节个数(utf-8一个汉字代表3个字节,gbk为2个字节)
concat 拼接字符串
substr和substring在MySQL用法中一样
instr 返回子串第一次出现的索引,如果找不到返回0
trim 查询占用字符值
upper 变大写
lower 变小写
lpad 用指定的字符实现左填充指定长度
rpad 用指定的字符实现右填充指定长度
replace 替换
数学函数:
round 四舍五入
ceil 向上取整,返回>=该参数的最小整数
floor 向下取整,返回<=该参数的最大整数
truncate 小数截断位数
mod 取余
日期函数:
now 返回当前系统日期+时间
curdate 返回当前系统日期,不包含时间
curtime 返回当前时间,不包含日期
year
month
monthname 返回当前月份 英文名
day
hour
minute
second 秒
str_to_date 将字符通过指定的格式转换成日期
date_format 将日期转换成字符
其他函数:
distinct 去重
concat 连接
version
database
user
控制函数
if if else 的效果
case switch case 的效果
一、字符函数
SELECT LENGTH('john');
SELECT LENGTH('张三丰hahaha');
SHOW VARIABLES LIKE '%char%'
SELECT CONCAT(last_name,'_',first_name) 姓名 FROM employees;
SELECT UPPER('john');
SELECT LOWER('joHn');
SELECT CONCAT(UPPER(last_name),LOWER(first_name)) 姓名 FROM employees;
注意:索引从1开始
SELECT SUBSTR('李莫愁爱上了陆展元',7) AS out_put;
SELECT SUBSTR('李莫愁爱上了陆展元',1,3) AS out_put;
SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2))) AS out_put
FROM employees;
SELECT INSTR('杨不殷六侠悔爱上了殷六侠','殷八侠') AS out_put;
SELECT LENGTH(TRIM(' 张翠山 ')) AS out_put;
SELECT TRIM('aa' FROM 'aaaaaaaaa张aaaaaaaaaaaa翠山aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') AS out_put;
SELECT LPAD('殷素素',4,'*') AS out_put;
SELECT RPAD('殷素素',5,'ab') AS out_put;
SELECT REPLACE('周芷若周芷若周芷若周芷若张无忌爱上了周芷若','周芷若','赵敏') AS out_put;
二、数学函数
SELECT ROUND(-1.55);
SELECT ROUND(1.567,2);
SELECT CEIL(-1.02);
SELECT FLOOR(-9.99);
SELECT TRUNCATE(1.69999,1);
SELECT MOD(10,-3);
SELECT 10%3;
三、日期函数
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT YEAR(NOW()) 年;
SELECT YEAR('1998-1-1') 年;
SELECT YEAR(hiredate) 年 FROM employees;
SELECT MONTH(NOW()) 月;
SELECT MONTHNAME(NOW()) 月;
SELECT STR_TO_DATE('1998-3-2','%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年') 入职日期
FROM employees
WHERE commission_pct IS NOT NULL;
四、其他函数
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
五、流程控制函数
SELECT IF(10<5,'大','小');
SELECT last_name,commission_pct,IF(commission_pct IS NULL,'没奖金,呵呵','有奖金,嘻嘻') 备注
FROM employees;
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;
如果工资>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;
|