SQL(Structure Query Language)语言是结构化查询查询语言。SQL语言可以分类为:
//查询单个字段
select 字段名 from 表名;
//查询多个字段
select 字段名,字段名 from 表名;
//查询所有字段
select * from 表名;
//查询常量
select 常量值;//注意,字符型和日期型的常量需用单引号包围,数值型不需要
//查询函数
select 函数名(实参列表);
//查询表达式
select 100/321;
//给字段或表等起别名,其中as可以省略
select name as 姓名 , age as 年龄 from
students as 学生;
//去重查询
select distinct 字段名 from 表名
//+符号的作用只能用作加法运算
select 数值 + 数值;//直接运算
select 数值 + 字符;//先试图将字符转换为数值参与运算,转换失败则字符自动为0参与运算
select null + 值;//与null值相加的所有结果都为0
补充:
//concat函数,用于拼接字符
select concat(frist_name , last_name) as 姓名 from employees;
//ifnull函数,判断字段或表达式是否为null,为null则返回指定值
select ifnull(salary , 0) from employees; //工资为0则自动返回0
//isnull函数,用于判断字段或表达式是否为null,是则返回1,否则返回0
select isnull(salary) from employees;
条件查询
select 查询列表 from 表名 where 筛选条件
根据筛选条件来对原始表的数据进行过滤,查询到需要的数据
筛选条件的分类:
① 条件表达式:如 salary > 10000
条件运算符有:> , < , = , <= , >= , != , <> , <=>
② 逻辑表达式:salary > 1000 && salary < 12000
逻辑运算符有:|| , && , ! , and , not , or
③ 模糊查询:select name from employees where name like ‘%a%’;
like , between and , in , is null / is not null
%与_作为通配符使用,前者可以匹配任意多个字符,后者只能匹配任意单个字符
注意区分is null和 <=> 的区别
is null不能用于判断是否为某普通类型的值,只能用于判断是否为空
<=>则可以用于判断普通类型值或null值
排序查询
select 查询列表 from 表名 order by 排序条件【asc|desc】
排序列表支持单个字段、多个字段、函数、表达式、别名。order by语句的位置一般在查询语句的最后(除limit语句以外)
示例:
select 查询列表 from 表名 where 筛选条件 order by 排序条件;
concat()//拼接字符串
select concat('ni' , ' ' , 'hao!');
substr()//截取字串
select substr('123456789' , 6);//注意SQL语言中的字符串索引从1开始
upper()//转换成大写
select upper('join');
lower()//转换成小写
select lower('join');
trim()//去掉字符串前后指定的空格或字符
select trim('a' from 'aaa1234sasaa8apaa');
ltrim()//去掉左边空格
rtrim()//去掉右边空格
replace()//替换
select replace('123456' , '4' , '9');//把串中4替换为9
lpad()//左填充
select lpad('12345' , 10 , '*');//使用指定字符填充指定位数:*****12345
rpad()//右填充
instr()//返回字串第一次出现的索引
select instr('1235690' , '90');
length()//获取字节个数
select length('woaibeijingtiananmen');
(2)数学函数
round()//对数据进行四舍五入处理
select round(87.75);//四舍五入取整
select round(19.2588 , 2);//四舍五入取指定位数:19.26
rand()//随机数函数
select round();//获取0-1内的指定浮点数
floor()//对数据进行向下取整
select floor(16.9330);
ceil()//对数据进行向上取整
select ceil(15.1);
mod()//取余
select mod(15,2);//相当于select 15%2;
truncate()//截断
select truncate(15.666666,3);//结果为15.666,直接截断,并不会进行舍入操作
(3) 日期函数
now()//当前系统日期 + 时间
select now();
select year(now());
select month(now());
curdate()//当前系统日期
select curdate();
curtime()//当前系统时间
select curtime();
str_to_date//将字符串转换成指定格式的日期
select str_to_date('9-13-1999' , '%m-%d-%Y');
date_format//将日期转换为字符串
select date_format(curdate(),'%Y年%m月%d日');//将日期转换为指定格式的字符串
(4)流程控制函数
if函数//处理双分支
case函数//处理多分支
(5)其他函数
version()//返回当前版本
database()//返回当前所在库
user()//返回当前连接用户
二、分组函数
sum()
select sum(salary) from employees;
max()
select max(salary) from employees;
min()
avg()
count()
select count(employee_id) from employees;
select count(*) from employees;
特点:①以上五个分组函数除了count()之外全部忽略null值;②sum和avg一般用处理数值型,max,min,count可以处理任何数据类型;③都可以搭配distinct使用,用于统计去重后的结果;④count的参数可以支持字段、常量值(1),不过建议使用count(*)
select count(*) , e.department_id
from employees e
where e.salary > 12000
group by e.department_id
having e.employee_id > 100
特点:①可以按单个字段分组;②和分组函数一同查询的最好是分组后的字段;③分组前筛选,针对原始表,使用where关键字;分组后筛选,针对分组后的结果集,使用having关键字;④可以按多个字段分组,字段之间使用逗号隔开;⑤支持排序;⑥having和group by后可以使用别名
//内连接
select bo.name as 男生姓名, b.name as 女生姓名
from boys as bo
join beauty as b on b. boyfriend_id = bo.id
where bo.id > 10;
//自连接
select e.last_name , m.last_name
from employees e
join employee m
on e.employees_id = m.manager_id;
特点:内连接中表的顺序可以调换,内连接的结果 = 多表的交集,n表连接至少要有n-1个连接条件
② 外连接
select 查询列表 from 表1 别名
left|right|full 【outer】 join 表2 别名 on 连接条件
where 筛选条件
group by 分组条件
having 分组后的筛选条件
order by 排序列表
示例
select b.name , bo.name
from beauty b
left join boys bo
on bo.id = b.boyfriend_id
where b.id > 10;
特点:查询的结果为主表中的所有行,如果从表的和它匹配精会显示匹配行,如果从表没有与其匹配的数据则显示null;left join 左边的表即为主表,反之亦然;外连接查询一般用于查询除了交集部分剩余不匹配的行
③ 交叉连接
select 查询列表 from 表1 别名
cross join 表2 别名
类似于笛卡尔乘积
select name
from (select * from employees
where salary > 12000) as ag
where id > 100;
③where或者having后面
支持标量子查询,行子查询,列子查询
实例:
select * from employees
where name in (
select name from employees
where salary > 1000
)
SELECT COUNT(*) , department_id
FROM employees
WHERE department_id != 10
GROUP BY department_id
HAVING department_id IN (SELECT e.department_id FROM employees e
WHERE salary > 10000)
select 查询列表
from 表
limit offset , size;
//offset表示其实的条目索引,默认从0开始,size代表显示的条目数
select * from employees
limit 0 , 10 ;
假设要显示的页数为page,每页条目数为size
select 查询列表
from 表
limit size*(page-1),size
select 查询列表|常量|函数 【from 表】 【where 条件】 union 【all】
select 查询列表|常量|函数 【from 表】 【where 条件】 union 【all】
select 查询列表|常量|函数 【from 表】 【where 条件】 union 【all】
......
select last_name from employees union all
select first_name from employees union all
select department_id from departments
多条查询语句查询的列类型应当一致;多条查询语句的列数必须一致;union代表去重显示,union all代表不去重显示。灵活使用联合查询可以将一条比较复杂的查询语句拆分成多条查询语句,适用于查询多个表中基本相同的列时使用。
故DQL语言中的要素集结为:
select 查询列表 from 表 别名
连接类型 join 表2 别名 on 连接条件
where 筛选条件
group by 分组条件
having 分组后筛选条件
order by 排序列表
limit 起始条目索引, 条目数;