一.插入数据
数据准备
- create table student(
- id INT,
- sn INT comment '学号',
- name VARCHAR(20) comment '姓名',
- qq_mail VARCHAR(20) comment 'QQ邮箱'
- );
复制代码 1.单行数据+全列插入
- INSERT INTO student VALUES (100, 10000, '唐三藏', NULL);
- INSERT INTO student VALUES (101, 10001, '孙悟空', '11111');
复制代码 这两条语句使用INSERT INTO ... VALUES语法向student表插入数据。要求VALUES后面的值 /列表数量温次序必须与表定义的列一致
2.多行数据+指定列插入 (推荐)
- INSERT INTO student (id, sn, name) VALUES
- (102, 20001, '曹孟德'),
- (103, 20002, '孙仲谋');
复制代码 这两条语句使用insert into..(列名列表)values(),();语法,指定了要插入数据的列是id、sn和name。这种方式可以只插入部门列的数据,并且允许按照指定列的次序插入值
二.查询
准备数据
- /创建考试成绩表
- 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);
复制代码 1.全列查询
- SELECT * FROM exam_result;
复制代码
2.指定列查询
- / 指定列的顺序不需要按定义表的顺序来
- SELECT name,math FROM exam_result;
复制代码
3.查询字段为表达式
3.1表达式不包含字段
- SELECT id, name, 10 FROM exam_result;
复制代码 此查询从 exam_result 表中选取 id 和 name 列,同时在效果集中添加一个常量列,该列的值始终为 10。 也就是说,查询效果里每一行的这个新列的值都是 10,不会随表中数据变革。
3.2 表达式包含一个字段
- SELECT id, name, english + 10 FROM exam_result;
复制代码 这个查询从 exam_result 表选取 id 和 name 列,并且计算 english 列的值加上 10 后的效果。查询效果里会有一个新列,该列的值是 english 列的值加上 10。
3.3 表达式包含多个字段
- SELECT id, name, chinese + math + english FROM exam_result;
复制代码 3.4 注意事项
1.若 english、chinese 大概 math 列存在 NULL 值,在举行加法运算时,效果会是 NULL。若要处置惩罚 NULL 值,可以使用 COALESCE 函数把 NULL 替换成 0,示比方下:
- SELECT id, name, COALESCE(chinese, 0) + COALESCE(math, 0) + COALESCE(english, 0) FROM exam_result;
复制代码 coalesce
4.别名
对于计算出的新列,若想给它起一个故意义的别名,可使用 AS 关键字,示比方下:
- select id,name,chinese+math+english as total_score
复制代码
5.去重—distinct关键词
问题:98分重复了
代码示例:
- select distinct math from exam_result;
复制代码 效果:
6. 排序order by
ASC 为升序(从小到大)
DESC 为降序(从大到小)
默以为 ASC
- -查询同学姓名和 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
复制代码 6.1使用表达式及别名排序
- select name,chinese+english+math as total from exam_result order by total desc;
复制代码
6.2 多个字段举行排序
排序优先级随书写次序
- -查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
- select name,math,english,chinese from exam_result order by math desc,english ,chinese;
复制代码
7.条件查询:WHERE
1.基本查询
WHERE条件可以使用表达式,但不能使用别名。
- -查询英语不及格的同学及英语成绩 ( < 60 )
- SELECT name, english FROM exam_result WHERE english < 60;
- -查询语文成绩好于英语成绩的同学
- SELECT name, chinese, english FROM exam_result WHERE chinese > english;
- -查询总分在 200 分以下的同学
- SELECT name, chinese + math + english FROM exam_result
- WHERE chinese+math+english < 200;
复制代码 2.AND/OR
1.对照
AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部门
1.示例
- -查询语文成绩大于80分,且英语成绩大于80分的同学
- SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
- -查询语文成绩大于80分,或英语成绩大于80分的同学
- SELECT * FROM exam_result WHERE chinese > 80 or english > 80;
复制代码
2.示例
- -观察AND 和 OR 的优先级:
- 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;
复制代码
3.范围查询:between....and
- -查询语文成绩在 [80, 90] 分的同学及语文成绩
- select name,chinese from exam_result where chinese between 80 and 90;
- -使用and也可以实现
- select name,chinese from exam_result where chinese>=80 and chinese<=90;
复制代码 4.范围查询:in
- - 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
- 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;
复制代码 5.模糊查询:like
- - % 匹配任意多个(包括 0 个)字符
- SELECT name FROM exam_result WHERE name LIKE '孙%';-- 匹配到孙悟空、孙权
- -_ 匹配严格的一个任意字符
- SELECT name FROM exam_result WHERE name LIKE '孙_';-- 匹配到孙权
复制代码
6.NULL 的查询:is ( not ) null
- -查询 qq_mail 已知的同学姓名
- select name from exam_result where qq_mail is not null;
- -查询 qq_mail 未知的同学姓名
- select name from exam_result where qq_mail is null;
复制代码 8.分页查询
三.修改
- - 将孙悟空同学的数学成绩变更为 80 分
- UPDATE exam_result SET math = 80 WHERE name = '孙悟空';
- - 将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
- UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
- - 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
- UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
- 3;
- - 将所有同学的语文成绩更新为原来的 2 倍
- UPDATE exam_result SET chinese = chinese * 2;
复制代码 四.删除
- - 删除整表数据
- DELETE FROM for_delete;
复制代码- - 删除孙悟空同学的考试成绩
- DELETE FROM exam_result WHERE name = '孙悟空';
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |