MySQL从删库到跑路(一)

MySQL数据库指令集

增(insert)

  • 不指定字段
insert into <表名> values(值1,值2,值3...);
  • 指定字段(没给到值的字段为默认值或null)
insert into <表名>[(字段1,字段2,字段3,...)] values(值1,值2,值3...);
  • insert与子查询(插入多条数据)
insert into <表名> 
<子查询>;
  • replace插入

用法与insert基本相同,如:replace into <表名> values(值1,值2,值3...);,不一样的是若是发现表中已经有此行数据(根据主键或者惟一索引判断)则先删除此行数据,而后插入新的数据。不然,直接插入新数据。
  注意:由于要根据主键或者是惟一索引判断是否有重复数据,因此操做的表必需要有主键或者是惟一索引。不然的话,replace into 会直接插入数据。数据库


删(delete)

  • 经常使用删除语句
delete from <表名> [where condition];
  • delete与子查询(删除多条数据)
delete from <表名> 
where 字段=<子查询>;
  • truncate清空表记录
truncate table <表名>;

改(update)

  • 经常使用更新语句
update <表名> set 字段1=值1,字段2=值2...
[where condition];
  • update与子查询(修改多条数据)
update <表名> set 字段1=值1,字段2=值2...
where 字段=<子查询>;

查(select)

  • 经常使用查询语句
select [distinct] <字段名或表达式>[,<字段名或表达式>]
from <表名或视图名>[,<表名或视图名>]
[where <条件表达式>]
[group by <字段名>[having <条件表达式>]]
[order by<字段名>[asc|desc]]
[limit [start,]count]
  • distinct关键字

这个关键字来过滤掉多余的重复记录只保留一条,但每每只用它来返回不重复记录的条数,而不是用它来返回不重记录的全部值。其缘由是distinct只能返回它的目标字段,而没法返回其它字段函数

  • [where <条件表达式>]

1.关系表达式查询
关系运算符:=(等于)、>(大于)、<(小于)、>=(大于等于)、<=(小于等于)、!=或<>(不等于)
eg:select name from user where id>10;code

2.逻辑表达式查询
逻辑运算符(优先级从高到低):not、and、or
eg:select * from user where name='simu' and age=20;对象

3.设置取值范围的查询
谓词:between ... and ... 或 not between ... and ...
eg:select * from user where id between 10 and 20;排序

4.空值查询
谓词:is null 或 is not null
eg:select * from user where id is null;索引

5.模糊查询
SQL通配符:
一、%:表明任意多个字符。
二、_(下划线):表明任意一个字符。
谓词:like 或 not like
eg:select * from user where name like 'simu';it

  • 分组查询-[group by <字段名>[having <条件表达式>]]

聚合函数io

SUM():返回某列全部值的总和
AVG():返回某列的平均值
MAX():返回某列的最大值
MIN():返回某列的最小值
COUNT():返回某列的行数

group by 子句:将根据所指定的列对结果集中的行进行分组。
having 子句:以组为对象进行筛选。
eg:select role count(*) as 总数 from user group by role having count(*)>0table

  • [order by<字段名>[asc|desc]]

根据查询结果中的一个或多个字段对查询结果进行排序。默认升序asc;
eg:select name,money from user order by money desc;date

  • [limit [start,]count]

从结果集中进一步选取指定数量的数量,默认值为0,即第1行数据为0。
eg:select name,money from user order by money desc limit 5;

相关文章
相关标签/搜索