语法:
insert into 表名 values(字段,字段,...);

现有student表:
create table student (name varchar(50),id int);
insert into student values ('Wendy',20);

insert into student (列名,列名..) values (..),(...);

select * from 表名;

所有查询出来的结果是一个临时表,并不会改变原表的内容
以下介绍的字句都适用于select查询语句中
select 列名 ,列名,..from 表名;

select 表达式 from 表名;

表达式包含一个字段:给math加10分;
查询出来的临时表math加了10分,但是原表内容并没有改变

表达式包含多个字段

为查询结果中的列指定一个小名,以小名作为该列的名称:
as 也可以省略
select 列名 as ,列名 as ... 别名 from 表名;

使用distinct 关键字,对于查询的指定列去重
select distinct 列名 from 表名;

select 列名,列名...from 表名 order by 列名,列名... asc/desc;
order by字句的查询,返回的查询结果顺序是未定义的;NULL数据,视为比任何值都小,升序出现最上面,降序出现最下面;

比较运算符:
| 运算符 | 说明 |
|---|---|
| >,>=,<,<= | 大于,大于等于,小于,小于等于 |
| = | 等于,NULL不安全,如:NULL = NULL 结果是NULL |
| <=> | 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |
| !=,<> | 不等于 |
| between a and b | 范围匹配,[a, b],如果 a <= value <= b,返回 TRUE(1) |
| in (option,…) | 如果是 option 中的任意一个,返回 TRUE(1) |
| IS NULL | 是 NULL |
| IS NOT NULL | 不是 NULL |
| LIKE | 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字 |
=来判断是否为NULL逻辑运算符:
| 运算符 | 说明 |
|---|---|
| AND | 多个条件必须都为 TRUE(1),结果才是 TRUE(1)【类似 &&,并且】 |
| OR | 任意一个条件为 TRUE(1), 结果为 TRUE(1) 【类似 ||,或者】 |
| NOT | 条件为 TRUE(1),结果为 FALSE(0) |
and 和or 示例:
-- 查询 math 数学不及格 并且 English 英语也不及格的同学
select name,math,English from student where math < 60 and English < 60;
--查询 数学 比 英语 好的同学
select name,math,English from student where math > English;
--查询 总分在 150 分以下的同学 或者 英语不及格的同学
select name,math , English from student where math+English < 150 or English < 60;
范围查询 between …and…
-- 查询数学成绩在[60,80]分的同学
select name,math from student where math between 60 and 80;
-- 或者使用and运算符
select name,math from student where math >=60 and math <= 80;
in:
-- 查询数学成绩是 60 或者 70 或者 90 的同学
select name,math from student where math in (60,70,90);
--或者使用 or 运算符
select name,math from student where math = 60 or math = 70 or math = 90;
模糊查询:like
-- % 匹配任意个字符(包括0个或多个字符)
select name from student where name like '帅%';
-- _ 匹配一个任意字符(只有一个字符)
select name from student where name like '帅_';
_NULL的查询,is [not] null
-- 查询 math成绩 已知的同学
select name,math from student where math is not null;
-- 查询math成绩未知的同学
select name,math from student where math is null;
语法
select 列名... from 表名 limit s,n;//从第s条开始筛选n条结果.
select 列名... from 表名 limit n offset s;//同上
-- 第一页
select id,name math,from student order by id limit 0,3;//从0下标开始,筛选3条结果
-- 第二页
select id,name,math from student order by id limit 3, 3;//从3下标开始,帅选3条结果
-- 第三页
select id,name,math from student order by id limit 3 offset 6;//从6下标开始,筛选3条结果
-- 如果结果不足3个,不会有影响
语法:
update 表名 set 列名 = XXX ,列名 = XXX,....
示例:
-- 将 pop 同学的数学成绩 改为 100
update student set math = 100 where name = 'pop';
-- 将 总成绩倒数前三的 同学数学成绩加上30
update student set math = math + 30 order by math+English desc limit 3;
-- 将 所有数学成绩 改为 100
update student set math = 100;
语法
delete from 表名 (where ..../ order by..../ limit....);
-- 删除 pop同学的信息
delete from student where name = 'pop';
-- 删除 student表;
delete from student ;