基本语句结构:
- select[字段表达式][from子句][group by 子句][having 子句][order by 子句][limit 子句]
- 字段表达式:
- select 1+1
- select now()
- select unix_timestamp()
- select 字段*10 from 表名
2. as起别名:select 1+1 as a
from子句:
- from + 列表名
- from + 列表名1,列表名2(结果为笛卡尔积)
- select now() from dual (dual为虚拟表)
where 子句
- like模糊查询%和_
- not like
- between ... and ...
- in 包含
- not in 不包含
- is null 空值判断 isnull()
- is not null
grounp by 子句
合计函数
- count()查询记录不为null的数
- sum()统计字段和
- avg()统计字段平均值
- max()
- min()
- group_concat()连接组内字段
分组排序
- desc 降序
- asc 升序
多字段分组
- with rollup可以回滚查看
having子句
- where是从数据源检索
- having是得到结果后检索
order by排序子句
- order by 字段名 [asc|desc], [字段名 [asc|desc],]
limit子句
- limit start ,size
子查询,sub-query
-
select * from student where height= (select max(height) from student2)
子查询分类:
- 根据子查询出现位置分类
- where 型子查询
- from型
- 根据返回的数据格式
- 标量子查询 一个数据
- 列子查询 一列
- 行子查询 一行
- 表子查询 一个二维表
exists(子查询)
join查询 : form 表1 join 表2 on 连接条件
内连接: inner join 外连接:左外连接 右外连接:left join right join
交叉连接:cross join
自然连接: natural join