基础
- distinct 去重复
select distinct 列 from 表 where - order by 排序
select 列 from 表 order by 需要排序的列 (desc) - group by 分组,结合合计函数一起使用
select 合计函数 from 表 where 条件 group by 需要分组的列 - insert into 增
insert into 表 values 值
insert into 表 列 values 值 - update 改
update 表 set 列=值 where 列=值 - delect
delect from 表 where 列=值
进阶
- limit 规定返回的值的数量
select 列 from 表 limit 返回的数目 - like 搜索列中的指定模式
select 列 from where 列 like ‘a%’
% _ [字符列表] [^字符列表] [!字符列表] - in 规定多个值
select 列 from 表 where 列 in (值1,值2…)
not in (值1,值2…) - between and
select 列 from 表 where 列 between 值1 and 值2
not between 值1 and 值2 - union union all 合并结果集
union 默认去重复
union all 返回所以的值 - select into 创建表的备份
select 列 into 新表 from 旧表 where 条件 - create database 创建数据库
create database 数据库名 - create table 创建表
create table 表名 (列 数据类型,列 数据类型,…) - create index 创建索引
create index 表名 on 表名(规定需要索引的列名) - drop 删除索引、表、数据库
- alter table 在表中添加、修改和删除列
- auto increment 新数据插入表的时候生成唯一的数字
- view 视图
- date 日期函数
- unll unll函数
函数
select function(列) from 表
- 函数只能放在select、where中子查询的 select、having子句中
avg() 返回某列的平均值,unll不计入
count() 返回某列的行数
count(*) 返回被选行数
max() 返回某列的最高值
min() 返回某列的最低值
sum() 返回某列的总和 - having子句 解决where 关键字不能和合计函数一起使用
select 合计函数(列) from 表 where 条件 group by 需要分组的列 having 合计函数(列) 运算符 值
子查询
- 用圆括号包裹,子查询返回多个结果时,用ANY、SOME或ALL修饰比较运算符
select * from 表 where 列 != all (select from where)
join 连接
- SELECT 列名称 FROM 表1 inner join 表2 ON 表1的某列 = 表2的某列
inner join 内连接 一对一
left join 左连接 多对一
right join 右连接 一对多
full join 全连接 多对多