现在在persons表中新增数据
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value]
//-- 插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
//注意日期格式
insert into persons values('张三',21,'男',180.4,'2000-03-06');
insert into persons values('李四',20,'男',181.65,'2001-05-09');
insert into persons(name,age,sex) values
('曹操',52,'男'),
('大乔',63,'女');
没有指定的数据默认为NULL。
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT
//select * from 表名
select * from persons;
select name from persons;
select name,age from persons;
//也可以为列表起别名
select name as 名字 from persons;
select name 名字 from persons;
-- 表达式不包含字段
SELECT id, name, 10 FROM student;
-- 表达式包含一个字段
SELECT id, name, english + 10 FROM student;
-- 表达式包含多个字段
SELECT id, name, chinese + math + english FROM student;
SELECT id, name, chinese + math + english 总分 FROM student;
select distinct math from student;
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
//没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序
//NULL 数据排序,视为比任何值都小,升序出现在最上面,降序出现在最下面
select id,name,math from student order by math desc; //降序
select id,name,math from student order by math asc; //升序
select id,name,math + english + chinese from student order by math + english + chinese desc;
//起别名
select id,name,math + english + chinese 总分 from student order by 总分 desc;
运算符 | 说明 |
---|---|
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL |
<=> | 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |
!=, <> | 不等于 |
BETWEEN a0 AND a1 | 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) |
IN (option, …) | 如果是 option 中的任意一个,返回 TRUE(1) |
IS NULL | 是 NULL |
IS NOT NULL | 不是 NULL |
LIKE | 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符 |
运算符 | 说明 |
---|---|
AND | 多个条件必须都为 TRUE(1),结果才是 TRUE(1) |
OR | 任意一个条件为 TRUE(1), 结果为 TRUE(1) |
NOT | 条件为 TRUE(1),结果为 FALSE(0) |
//查询英语不及格的同学及英语成绩 ( < 60 )
SELECT name, english FROM student WHERE english < 60;
//查询语文成绩好于英语成绩的同学
SELECT name, chinese, english FROM student WHERE chinese > english;
//查询总分在 200 分以下的同学
SELECT name, chinese + math + english 总分 FROM student
WHERE chinese + math + english < 200;
//查询语文成绩大于80分,且英语成绩大于80分的同学
SELECT * FROM student WHERE chinese > 80 and english > 80;
// 查询语文成绩大于80分,或英语成绩大于80分的同学
SELECT * FROM student WHERE chinese > 80 or english > 80;
// 观察AND 和 OR 的优先级:
SELECT * FROM student WHERE chinese > 80 or math>70 and english > 70;
SELECT * FROM student WHERE (chinese > 80 or math>70) and english > 70;
//BETWEEN AND
//查询语文成绩在 [80, 90] 分的同学及语文成绩
SELECT name, chinese FROM student WHERE chinese BETWEEN 80 AND 90;
//使用 AND 也可以实现
SELECT name, chinese FROM student WHERE chinese >= 80 AND chinese
<= 90;
// IN
//查询数学成绩是 65 或者 73 或者 98 分的同学及数学成绩
SELECT name, math FROM student WHERE math IN (65, 73, 98);
//使用 OR 也可以实现
SELECT name, math FROM student WHERE math = 65 OR math = 73 OR math
= 98;
//% 匹配任意多个(包括 0 个)字符
SELECT name FROM student WHERE name LIKE '孙%';
// _ 匹配严格的一个任意字符
SELECT name FROM student WHERE name LIKE '孙_';
//查询 math已知的同学姓名
SELECT name, math FROM student WHERE math IS NOT NULL;
// 查询 math未知的同学姓名
SELECT name, math FROM student WHERE math IS NULL;
//从 0 开始,筛选 3 条结果
SELECT id,name,math FROM student WHERE math > 70 ORDER BY math desc LIMIT 3;
//从 2 开始,筛选 5 条结果
SELECT id,name,math FROM student LIMIT 2, 5;
//从 2 开始,筛选 5 条结果,比第二种用法更明确,建议使用
SELECT id,name,math FROM student LIMIT 5 OFFSET 2;
//语法
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
//示例
//将孙悟空同学的数学成绩变更为 80 分
UPDATE student SET math = 80 WHERE name = '孙悟空';
//将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
UPDATE student SET math = 60, chinese = 70 WHERE name = '曹孟德';
// 将总成绩倒数前三的 3 位同学的数学成绩加上 5 分
UPDATE student SET math = math + 5 ORDER BY chinese + math + english LIMIT 3;
//语法
DELETE FROM student [WHERE ...] [ORDER BY ...] [LIMIT ...]
//示例
//删除孙悟空同学的考试成绩
DELETE FROM student WHERE name = '孙悟空';
-- 删除整表数据
DELETE FROM student;