添加
insert into <表名> values(<添加的值1>,<添加的值2>,<......>);
删除
delete from <表名> where <条件> 【and or】 <条件>;
清空表数据
truncate table <表名>;
更新
update <表名> set <更新内容>,<更新内容> where <更新条件> 【and or】 <更新条件>;
查询
常见查询
select * from <表名> where <条件> 【and or】 <条件>;
字段别名
select 【<字段名> as <别名> or <字段名> <别名>】 from <表名>;
“as”可以省略
总数据
select count(*) from <表名> where <条件>;
和
select sum(<数值类型的字段名>) from <表名>;
平均
select avg(<数值类型的字段名>) from <表名>;
分页
select * from <表名> limit <开始行>,<每页展示的数量>;
查询指定行
select * from <表名> where <条件> 【and or】 <条件> limit <开始行>,<每页展示的数量>;
select * from <表名> where <条件> 【and or】 <条件> limit <每页展示的数量>;
查询前几行数据
分组
select * from <表名> group by <字段名>;
select * from <表名> where <条件> 【and or】 <条件> group by <字段名>;
select group_concat(<字段名>),<字段名>,<...> from <表名> where <条件> 【and or】 <条件> group by <字段名>;
group_concat()展示分组的全部数据
select count(<字段名>),group_concat(<字段名>),<字段名>,<...> from <表名> where <条件> 【and or】 <条件> group by <字段名>;
count()计算分组后总数据
select group_concat(<字段名>) from <表名> group by <字段名> with rollup;
最后一列显示出所有数据
排序
select * from <表名> order by <字段名> 【asc or desc】;
select * from <表名> where <条件> order by <字段名> 【asc or desc】;
select * from <表名> order by <字段名1> 【asc or desc】,<字段名2> 【asc or desc】;
先对"字段名1"排序,再对"字段名2"排序<br>
去重
select distinct <字段名> from <表名>;
模糊
select * from <表名> where <条件> like '%<匹配数据>_';
(%)模糊匹配 (_)匹配一个字符
范围
select * from <表名> where <字段名> between <开始范围> and <结束范围>;
过滤
select <过滤条件的字段名1> from <表名> having <字段名1><过滤条件>;
查询出的字段必须有过滤条件的字段名
分组过滤
select <过滤条件的字段名1>,<字段名> from <表名> group by <字段名> having <字段名1><过滤条件>;
条件分组过滤
select <过滤条件的字段名1>,<字段名> from <表名> where <条件> and <条件> group by <字段名> having <字段名1><过滤条件>;
分组过滤排序
select <字段名1>,<字段名> from <表名> where <条件> and <条件> group by <字段名> having <字段名1><过滤条件> order by <字段名>;
where>group by>having>order by>limit<br>
内连接
select * from <表名1> inner join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select * from <表名1> join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select <表名1>.<表名1中的字段>,<表名2>.<表名2中的字段> from <表名1> join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select <别名1>.<别名1中字段>,<别名2>.<别名2中字段> from <表名1> <别名1> join <表名2> <别名2> where <别名1>.<外键>=<别名2>.<主键>;
外连接
左连接
把inner join 换成 left join
右连接
把inner join 换成 right join
子查询
select * from <表名1> where <字段名1> 【<in> or <not in>】(select <字段名1> from <表名2> where <条件> and <条件>) and <条件>;