【MySQL】基础查询
文章目录
- 👉表的增删改查👈
- Create
- Retrieve
👉表的增删改查👈
CRUD 是数据库中常用的术语,表示对数据进行增、删、改、查的操作。具体来说,CURD 语句分为以下四种:
- Create(创建):用于创建新表、新列或新的约束等数据库对象。
- Retrieve(读取):用于查询表中的数据。
- Update(更新):用于更新表中的数据。
- Delete(删除):用于删除表中的数据。
Create
- 语法
insert [into] table_name
[(column [, column] ...)]
values (value_list) [, (value_list)] ...
value_list: value, [, value] ...
注:使用 insert 语句前,表必须要存在。
案例
create table if not exists students(
id int unsigned primary key auto_increment,
sn int unsigned unique key not null comment "学生的学号",
name varchar(64) not null comment "学生的姓名",
qq varchar(64) unique key );
单行数据+全列插入
#默认全列插入
mysql> insert into students values(1, 1234, '张飞', '1234@qq.com');
Query OK, 1 row affected (0.00 sec)
mysql> select * from students;
+----+------+--------+-------------+
| id | sn | name | qq |
+----+------+--------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
+----+------+--------+-------------+
1 row in set (0.00 sec)
注意:
- 插入数据记录时,value_list 数量必须和定义表的列的数量及顺序一致。
- 在插入数据记录的时候,也可以不用指定 id(当然,那时候就需要明确插入数据到哪些些列了),那么 mysql 会使用默认的值进行自增。
多行数据+指定列插入
mysql> insert into students (sn, name, qq) values
-> (1235, '关羽', '1235@qq.com'),
-> (1236, '赵云', '1236@qq.com');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from students;
+----+------+--------+-------------+
| id | sn | name | qq |
+----+------+--------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
| 3 | 1235 | 关羽 | 1235@qq.com |
| 6 | 1236 | 赵云 | 1236@qq.com |
+----+------+--------+-------------+
3 rows in set (0.00 sec)
插入否则更新
主键或者唯一键对应的值已经存在会导致插入数据记录失败。
# 主键和唯一键冲突
mysql> insert into students values(1, 1237, '刘备', '1237@qq.com');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into students values(7, 1236, '刘备', '1236@qq.com');
ERROR 1062 (23000): Duplicate entry '1236' for key 'sn'
同步更新操作语法:
insert... on duplicate key update
column = value [, column = value] ..
-- on duplicate key 当发生重复key的时候,进行更新
mysql> insert into students (id, sn, name) values(6, 1238, '刘备') on duplicate key update sn=1238, name='刘备';
Query OK, 2 rows affected (0.00 sec)
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
mysql> select * from students;
+----+------+--------+-------------+
| id | sn | name | qq |
+----+------+--------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
| 3 | 1235 | 关羽 | 1235@qq.com |
| 6 | 1238 | 刘备 | 1236@qq.com |
+----+------+--------+-------------+
3 rows in set (0.00 sec)
mysql> insert into students (id, sn, name) values(6, 1238, '刘备') on duplicate key update sn=1238, name='刘备';
Query OK, 0 rows affected (0.00 sec)
#通过 MySQL 函数获取受到影响的数据行数
mysql> select ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
#如果更新的值其他具有唯一性的值冲突时,需要避免该冲突
mysql> insert into students (id, sn, name) values(6, 1235, '诸葛亮') on duplicate key update sn=1235, name='诸葛亮';
ERROR 1062 (23000): Duplicate entry '1235' for key 'sn'
mysql> insert into students (id, sn, name) values(6, 1235, '诸葛亮') on duplicate key update sn=1236, name='诸葛亮';
Query OK, 2 rows affected (0.01 sec)
mysql> select * from students;
+----+------+-----------+-------------+
| id | sn | name | qq |
+----+------+-----------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
| 3 | 1235 | 关羽 | 1235@qq.com |
| 6 | 1236 | 诸葛亮 | 1236@qq.com |
+----+------+-----------+-------------+
3 rows in set (0.00 sec)
替换
-- 主键 或者 唯一键 没有冲突,则直接插入;
-- 主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn, name) values(1237, '曹操');
Query OK, 1 row affected (0.00 sec)
mysql> select * from students;
+----+------+-----------+-------------+
| id | sn | name | qq |
+----+------+-----------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
| 3 | 1235 | 关羽 | 1235@qq.com |
| 6 | 1236 | 诸葛亮 | 1236@qq.com |
| 11 | 1237 | 曹操 | NULL |
+----+------+-----------+-------------+
4 rows in set (0.01 sec)
mysql> replace into students (sn, name) values(1237, '黄月英');
Query OK, 2 rows affected (0.01 sec)
mysql> select * from students;
+----+------+-----------+-------------+
| id | sn | name | qq |
+----+------+-----------+-------------+
| 1 | 1234 | 张飞 | 1234@qq.com |
| 3 | 1235 | 关羽 | 1235@qq.com |
| 6 | 1236 | 诸葛亮 | 1236@qq.com |
| 12 | 1237 | 黄月英 | NULL |
+----+------+-----------+-------------+
4 rows in set (0.00 sec)
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,删除后重新插入
Retrieve
select
[distinct] {* | {column [, column] ...} #去重
[from table_name]
[where ...] #查询条件
[order by column [asc | desc], ...] #升序/降序
limit ...#显式几行数据记录
案例
-- 创建表结构
CREATE TABLE exam_result (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT '同学姓名',
chinese float DEFAULT 0.0 COMMENT '语文成绩',
math float DEFAULT 0.0 COMMENT '数学成绩',
english float DEFAULT 0.0 COMMENT '英语成绩'
);
-- 插入测试数据
INSERT INTO exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);
-- 查看表数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.01 sec)
select 列
全列查询
- 通常情况下不建议使用 * 进行全列查询。
- 查询的列越多,意味着需要传输的数据量越大。
- 可能会影响到索引的使用。
# 全列查询
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.01 sec)
指定列查询
- 指定列的顺序不需要按定义表的顺序来。
mysql> select id from exam_result;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+----+
7 rows in set (0.00 sec)
mysql> select id, name, chinese from exam_result;
+----+-----------+---------+
| id | name | chinese |
+----+-----------+---------+
| 1 | 唐三藏 | 67 |
| 2 | 孙悟空 | 87 |
| 3 | 猪悟能 | 88 |
| 4 | 曹孟德 | 82 |
| 5 | 刘玄德 | 55 |
| 6 | 孙权 | 70 |
| 7 | 宋公明 | 75 |
+----+-----------+---------+
7 rows in set (0.00 sec)
查询字段为表达式
-- 表达式不包含字段
mysql> select name, id, 10 from exam_result;
+-----------+----+----+
| name | id | 10 |
+-----------+----+----+
| 唐三藏 | 1 | 10 |
| 孙悟空 | 2 | 10 |
| 猪悟能 | 3 | 10 |
| 曹孟德 | 4 | 10 |
| 刘玄德 | 5 | 10 |
| 孙权 | 6 | 10 |
| 宋公明 | 7 | 10 |
+-----------+----+----+
7 rows in set (0.00 sec)
mysql> select name, id, 10+10 from exam_result;
+-----------+----+-------+
| name | id | 10+10 |
+-----------+----+-------+
| 唐三藏 | 1 | 20 |
| 孙悟空 | 2 | 20 |
| 猪悟能 | 3 | 20 |
| 曹孟德 | 4 | 20 |
| 刘玄德 | 5 | 20 |
| 孙权 | 6 | 20 |
| 宋公明 | 7 | 20 |
+-----------+----+-------+
7 rows in set (0.00 sec)
-- 表达式包含一个字段(英语分数+10)
mysql> select id, name, english + 10 from exam_result;
+----+-----------+--------------+
| id | name | english + 10 |
+----+-----------+--------------+
| 1 | 唐三藏 | 66 |
| 2 | 孙悟空 | 87 |
| 3 | 猪悟能 | 100 |
| 4 | 曹孟德 | 77 |
| 5 | 刘玄德 | 55 |
| 6 | 孙权 | 88 |
| 7 | 宋公明 | 40 |
+----+-----------+--------------+
7 rows in set (0.00 sec)
-- 表达式包含多个字段(计算总分)
mysql> select id, name, chinese + math + english from exam_result;
+----+-----------+--------------------------+
| id | name | chinese + math + english |
+----+-----------+--------------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)
-- select也可以直接计算表达式
mysql> select 1+2+3+4+5;
+-----------+
| 1+2+3+4+5 |
+-----------+
| 15 |
+-----------+
1 row in set (0.00 sec)
为查询结果指定别名
SELECT column [AS] alias_name [...] FROM table_name;
mysql> select id, name, chinese+math+english as total from exam_result;
+----+-----------+-------+
| id | name | total |
+----+-----------+-------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+-------+
7 rows in set (0.00 sec)
mysql> select id 学号, name 名字, chinese+math+english 总分 from exam_result;
+--------+-----------+--------+
| 学号 | 名字 | 总分 |
+--------+-----------+--------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+--------+-----------+--------+
7 rows in set (0.00 sec)
结果去重
- 一行数据记录完全相同才能被看做是重复的数据记录
-- 98分重复了
mysql> select math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
-- 去重结果
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec
where 条件
比较运算符
逻辑运算符
数学成绩大于等于 80 分的同学及数学成绩
mysql> select name, math from exam_result where math>=80;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
| 曹孟德 | 84 |
| 刘玄德 | 85 |
+-----------+------+
4 rows in set (0.00 sec)
数学成绩等于98分的同学
mysql> select name, math from exam_result where math=98;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)