目录
1.CRUD
2.新增数据
2.1单行数据+全列插入
2.2多行数据+指定列插入
3.查询数据
3.1全列查询
3.2指定列查询
3.3查询字段为表达式
3.4别名
3.5去重DISTINCT
3.6排序ORDER BY
3.7 条件查询:WHERE
3.8 分页查询:LIMIT
4.修改数据
5.删除数据
CRUD即:
增加(Create)
查询(Retrieve)
更新(Update)
删除(Delete)
补充:MySQL中的注释符:
单行注释符"#",注释符后直接加注释内容
单行注释符"--",
--
注释符后需要加一个空格,注释才能生效多行注释符"/* */",/*用于注释内容的开头,*/用于注释内容的结尾
语法:
INSERT [ INTO ] table_name[( column [, column ] ...)]VALUES (value_list) [, (value_list)] ...value_list: value,[,value]...
-- 创建一张学生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
id INT,
sn INT comment '学号',
name VARCHAR(20) comment '姓名',
qq_mail VARCHAR(20) comment 'QQ邮箱'
);
-- 插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
INSERT INTO student VALUES (100, 10000, '唐唐', '22222');
INSERT INTO student VALUES (101, 10001, '悟悟', '11111');
-- 插入两条记录,value_list 数量必须和指定列数量及顺序一致
INSERT INTO student (id, sn, name) VALUES
(102, 20001, '曹孟德'),
(103, 20002, '孙仲谋');
语法:
SELECT[ DISTINCT ] {* | {column [, column ] ...}[ FROM table_name ][ WHERE ...][ ORDER BY column [ ASC | DESC ], ...]LIMIT ...
-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
id INT,
name VARCHAR(20),
chinese DECIMAL(3,1),
math DECIMAL(3,1),
english DECIMAL(3,1)
);
-- 插入测试数据
INSERT INTO exam_result (id,name, chinese, math, english) VALUES
(1,'唐三藏', 67, 98, 56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98.5, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘玄德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);
select * from exam_result;
-- 指定列的顺序不需要按定义表的顺序来
SELECT id, name, english FROM exam_result;
-- 表达式不包含字段
SELECT id, name, 10 FROM exam_result;
-- 表达式包含一个字段
SELECT id, name, english + 10 FROM exam_result;
-- 表达式包含多个字段
SELECT id, name, chinese + math + english FROM exam_result;
表达式不包含字段:
表达式包含一个字段(可见每个name的English都加了10):
表达式包含多个字段:
SELECT column [ AS ] alias_name [...] FROM table_name;
语法:
-- ASC 为升序(从小到大)-- DESC 为降序(从大到小)-- 默认为 ASCSELECT ... FROM table_name [ WHERE ...]ORDER BY column [ ASC | DESC ], [...];
-- 查询同学姓名和 qq_mail,按 qq_mail 排序显示
-- 升序
SELECT name, qq_mail FROM student ORDER BY qq_mail;
-- 降序
SELECT name, qq_mail FROM student ORDER BY qq_mail DESC;
-- 查询同学及总分,由高到低
SELECT name, chinese + english + math FROM exam_result
ORDER BY chinese + english + math DESC;
SELECT name, chinese + english + math total FROM exam_result
ORDER BY total DESC;
-- 查询同学各门成绩,按数学降序,英语升序,语文升序的方式进行排序
select name,math,english,chinese from exam_result order by math desc,english,chinese;
比较运算符:
运算符 | 说明 |
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,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)
|
1. WHERE 条件可以使用表达式,但不能使用别名。2. AND 的优先级高于 OR ,在同时使用时,需要使用小括号 () 包裹优先执行的部分
SELECT name, english FROM exam_result WHERE english < 60;
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
SELECT name, chinese + math + english 总分 FROM exam_result
WHERE chinese + math + english < 200;
AND与OR:
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
SELECT * FROM exam_result WHERE chinese > 80 or english > 80;
SELECT * FROM exam_result WHERE chinese > 80 or math>70 and english > 70;
SELECT * FROM exam_result WHERE (chinese > 80 or math>70) and english > 70;
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;
SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese
<= 90;
2. IN
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);
SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math
= 98 OR math = 99;
SELECT name FROM exam_result WHERE name LIKE '孙_';-- 匹配到孙权
SELECT name, qq_mail FROM student WHERE qq_mail IS NOT NULL;
SELECT name, qq_mail FROM student WHERE qq_mail IS NULL;
-- 起始下标为 0-- 从 0 开始,筛选 n 条结果SELECT ... FROM table_name [ WHERE ...] [ ORDER BY ...] LIMIT n;-- 从 s 开始,筛选 n 条结果SELECT ... FROM table_name [ WHERE ...] [ ORDER BY ...] LIMIT s, n;-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用SELECT ... FROM table_name [ WHERE ...] [ ORDER BY ...] LIMIT n OFFSET s;
语法:
UPDATE table_name SET column = expr [, column = expr ...][ WHERE ...] [ ORDER BY ...] [ LIMIT ...]
示例:
UPDATE exam_result SET math = 80 WHERE name = '孙悟空';
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;
UPDATE exam_result SET chinese = chinese * 2;
语法:
DELETE FROM table_name [ WHERE ...] [ ORDER BY ...] [ LIMIT ...]
DELETE FROM exam_result WHERE name = '孙悟空';
DROP TABLE IF EXISTS for_delete;
CREATE TABLE for_delete (
id INT,
name VARCHAR(20)
);
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
DELETE FROM for_delete;