DQL语言
1.基础查询
select 查询列表 from 表名
查询列表:表中字段、常量值、表达式、函数;
起别名:
select 查询列表 as 别名 from 表名;
其中as可以省略
去重:
select distinct 查询列表 from 表名;
拼接:
select concat(列表1,列表2) as 别名 from 表名;
2.条件查询
select 查询列表
from 表名
where 筛选条件;
分类:
1.按条件表达式筛选
条件运算符:> < = != <> >= <=
2.按逻辑表达式筛选
逻辑运算符:&& || !
and or not
3.模糊查询
like between ... and
in is null
like:
%:任意多个字符
_:一个字符
3.排序查询
select 查询列表
from 表
where 筛选条件
order by 排序列表【asc/desc】;
4.分组函数
分类:
sum 求和
avg 平均
max 最大值
min 最小值
count 计算个数
5.分组查询
select 查询列表
from 表名
where 筛选条件
group by 分组表达式
order by 排序列表【asc/desc】
select 查询列表
from 表名
where 筛选条件 #根据原始表筛选
group by 分组表达式
having 筛选条件 #根据分组后的表进行筛选
order by 排序列表【asc/desc】
6.联接查询
sql92
select 查询列表
from 表1,表2
where 表1.属性=表2.属性;
and 筛选条件
group by 分组表达式
having 筛选条件
order by 排序列表【asc/desc】
sql99
内连接:
select 查询列表
from 表1 别名 连接类型
join 表2 别名
on 连接条件
where 筛选条件
group by 分组表达式
having 筛选条件
order by 排序列表【asc/desc】;
分类:
内连接:inner
外连接
左外:left 【outer】
右外:right 【outer】
全外:full 【outer】
交叉连接:cross
7.子查询
概念:出现其他语句内部的select语句,称为子查询或内查询
8.分页查询
select 查询列表
from 表名
join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后筛选
order by 排序字段
limit 起始索引,显示条目个数;
9.联合查询
union:将多条查询语句的结果合并成一个结果
查询语句1
union
查询语句2