MySQL:MySQL的查询(上)

本篇开始总结的是MySQL当中的基本查询语句

对于数据库的查询,无非大致就是增删查改,因此对于这些内容进行一一解释:

MySQL的增加

单行数据插入

mysql> create table students ( id int unsigned primary key auto_increment, name
 varchar(20) not null, qq varchar(20) );
Query OK, 0 rows affected (0.12 sec)

mysql> desc students;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)  | NO   |     | NULL    |                |
| qq    | varchar(20)  | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)

mysql> insert into students (name, qq) values ('小明', '123321');
Query OK, 1 row affected (0.01 sec)

mysql> insert into students  values (2, '小红', '1123321');
Query OK, 1 row affected (0.01 sec)

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小明   | 123321  |
|  2 | 小红   | 1123321 |
+----+--------+---------+
2 rows in set (0.00 sec)

在增加这个方面没有什么需要注意的,更多需要注意的是全列插入还是单列插入,如果指定了内容就是单列插入,如果在values前没有指定具体的内容,那就是全列插入

多行数据插入

insert语句也支持多行数据插入:

mysql> insert into students (name, qq) values ('小刚', 3333), ('小亮', 4444444);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小明   | 123321  |
|  2 | 小红   | 1123321 |
|  3 | 小刚   | 3333    |
|  4 | 小亮   | 4444444 |
+----+--------+---------+
4 rows in set (0.01 sec)

插入否则更新

在前面的学习中也知道了主键和唯一键,正常来说如果插入相同的数据到这当中是会提示有错误的信息的,为了方便描述更改一下列的信息:

mysql> alter table students add unique (qq);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc students;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)  | NO   |     | NULL    |                |
| qq    | varchar(20)  | YES  | UNI | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

进行主键和唯一键重复更新

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小明   | 123321  |
|  2 | 小红   | 1123321 |
|  3 | 小刚   | 3333    |
|  4 | 小亮   | 4444444 |
+----+--------+---------+
4 rows in set (0.00 sec)

mysql> insert into students (id, name, qq) values (1, '小纯', 123321);
ERROR 1062 (23000): Duplicate entry '1' for key 'students.PRIMARY'
mysql> insert into students (id, name, qq) values (1, '小纯', 123321) on duplicate key update id=1, name='小纯',qq=123321;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小纯   | 123321  |
|  2 | 小红   | 1123321 |
|  3 | 小刚   | 3333    |
|  4 | 小亮   | 4444444 |
+----+--------+---------+
4 rows in set (0.00 sec)

从上面所示的示意可以看出这两个的关系,当主键或者唯一键发生重复后,执行后面的语句可以直接进行替换

但是这样的方法还是比较繁琐,因为在MySQL中直接提供了进行替换的sql语句:

替换

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小纯   | 123321  |
|  2 | 小红   | 1123321 |
|  3 | 小刚   | 3333    |
|  4 | 小亮   | 4444444 |
+----+--------+---------+
4 rows in set (0.00 sec)

mysql> replace into students (id, name, qq) values (1, '小铁', 123321);
Query OK, 2 rows affected (0.01 sec)

mysql> select * from students;
+----+--------+---------+
| id | name   | qq      |
+----+--------+---------+
|  1 | 小铁   | 123321  |
|  2 | 小红   | 1123321 |
|  3 | 小刚   | 3333    |
|  4 | 小亮   | 4444444 |
+----+--------+---------+
4 rows in set (0.00 sec)

值得注意的是,当提示有Query OK, 2 rows affected (0.01 sec),这个2表示的就是有出现冲突的数据,并且发生了替换,这个所谓的2的意思就是删除了之后再重新插入,所以就是表示的是2行数据被修改

MySQL的查询

下面进行的是对于MySQL的查询模块

select列

一般来说尽量不要使用全列查询,因为可能数据量很大,会带来很大的问题,所以通常来说要选择的最好还是指定列

指定列查询

mysql> select id, name from students;
+----+--------+
| id | name   |
+----+--------+
|  1 | 小铁   |
|  2 | 小红   |
|  3 | 小刚   |
|  4 | 小亮   |
+----+--------+
4 rows in set (0.00 sec)

mysql> select name, id from students;
+--------+----+
| name   | id |
+--------+----+
| 小铁   |  1 |
| 小红   |  2 |
| 小刚   |  3 |
| 小亮   |  4 |
+--------+----+
4 rows in set (0.00 sec)

其中这个顺序是可以不保持创建顺序的,因为是对于一列一列的数据进行提取

表达式

在进行查询的时候也可以带有表达式,例如:

mysql> select id * 2 from students;
+--------+
| id * 2 |
+--------+
|      4 |
|      2 |
|      6 |
|      8 |
+--------+
4 rows in set (0.00 sec)

取别名

也可以对新的一列进行重命名的操作

mysql> select id * 2 as newid from students;
+-------+
| newid |
+-------+
|     4 |
|     2 |
|     6 |
|     8 |
+-------+
4 rows in set (0.00 sec)

mysql> select id * 2 newid from students;
+-------+
| newid |
+-------+
|     4 |
|     2 |
|     6 |
|     8 |
+-------+
4 rows in set (0.00 sec)

其中as可以省去

对于结果去重

利用distinct关键字可以对于结果进行去重的操作

mysql> select math from exam_result;
+------+
| math |
+------+
|   90 |
|   90 |
|   90 |
+------+
3 rows in set (0.01 sec)

mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   90 |
+------+
1 row in set (0.03 sec)

where语句

比较运算符:

在这里插入图片描述
结果排序

对于查询出的MySQL结果进行排序,常用的有asc表示升序,desc表示降序,默认采取的是降序排序

mysql> select math from exam_result order by math desc;
+------+
| math |
+------+
|   95 |
|   91 |
|   90 |
|   90 |
|   90 |
|   50 |
+------+
6 rows in set (0.00 sec)

mysql> select math from exam_result order by math asc;
+------+
| math |
+------+
|   50 |
|   90 |
|   90 |
|   90 |
|   91 |
|   95 |
+------+
6 rows in set (0.00 sec)

相关推荐

  1. mysqlySQL中启用慢查询日志并设置阈值

    2024-04-10 15:10:05       14 阅读
  2. Trino:分区表SQL提交 & 查询流程浅析

    2024-04-10 15:10:05       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 15:10:05       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 15:10:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 15:10:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 15:10:05       20 阅读

热门阅读

  1. 去了加拿大才能知道的一些事

    2024-04-10 15:10:05       15 阅读
  2. Eureka注册步骤

    2024-04-10 15:10:05       14 阅读
  3. LeetCode-热题100:23. 合并 K 个升序链表

    2024-04-10 15:10:05       11 阅读
  4. 计算机网络---第四天

    2024-04-10 15:10:05       15 阅读
  5. react类组件转化为函数组件

    2024-04-10 15:10:05       12 阅读
  6. docker 的常用命令

    2024-04-10 15:10:05       16 阅读
  7. torch.mean()的使用方法

    2024-04-10 15:10:05       13 阅读