select 查询列1,2,3…… rom 表--查询列中除聚合函数之外的列,应该列入group by子句中,否则会报错where 筛选列 操作符 值
groupby 分组列1,2,3…… with rollup--建议分组列写入查询列中,这样查询结果更清晰having 筛选列 操作符 值
orderby 排序列1,2,3…… asc/desc
多表查询
内连接方式1
select 查询列1,2,3…… from 表1,表2,表3……
where 多表连接条件 and 筛选列 操作符 值
groupby 分组列1,2,3…… with rolluphaving 筛选列 操作符 值
orderby 排序列1,2,3…… asc/desc--注:若列在多表中不唯一,必须写成:表名.列名或表的别名.列名
内连接方式2
select 查询列1,2,3…… from 表1[inner]join 表2on 连接条件
where 多表连接条件 and 筛选列 操作符 值
groupby 分组列1,2,3…… with rolluphaving 筛选列 操作符 值
orderby 排序列1,2,3…… asc/desc
外连接
select 查询列1,2,3…… from 表1left/right/full[outer]join 表2on 连接条件
where 多表连接条件 and 筛选列 操作符 值
groupby 分组列1,2,3…… with rolluphaving 筛选列 操作符 值
orderby 排序列1,2,3…… asc/desc
子查询
子查询返回单列
select …… from 表
where 筛选列 操作符 (子查询语句)
子查询返回多列
select …… from 表
whereexists(子查询)
5.表数据更新
--向表中添加记录--(1)逐条添加insert[into] 表名 [(列1,列2,列3……)]values(值1,值2,值3……),(值1,值2,值3……),(值1,值2,值3……)……
--(2)一次性添加子查询结果集insert[into] 表名 [(列1,列2,列3……)](子查询)--修改表中记录update 表名
set 修改列1=值,修改列2=值2……
where 筛选值 操作符 值
--删除表中记录delete[from] 表
where 筛选值 操作符 值
--truncate table语句与不带where的delete语句一样:清空表中记录;但truncate table执行速度更快,因为不需向日志文件写入太多的内容truncatetable 表名