MySQL基础04-数据简单查询(DQL语句)

一、简介

DQL(Data Query Language)数据查询语言
用来查询数据库中表的数据。关键词:selectwhere

二、简单查询

1)查询所有数据
select * from 表名;

2)查询指定字段
select 字段1,字段2,... from 表名;

3)别名查询
select 字段1 [as] 别名,字段2,... from 表名;

mysql> select id,name as 名字 from user;
+----+--------+
| id | 名字   |
+----+--------+
|  1 | 张三   |
|  2 | 大O    |
|  3 | 小四   |
|  4 | 小小   |
+----+--------+

4)查询时去除重复值
select distinct 字段1,字段2,... from 表名;
重复的值只会出现一次。

mysql> select * from user1;
+------+--------+
| id   | name   |
+------+--------+
|    2 | 小O    |
|    1 | 张三   |
|    2 | 大O    |
|    3 | 小四   |
|    4 | 小小   |
|    2 | 大O    |
+------+--------+
6 rows in set (0.00 sec)

mysql> select distinct id  from user1;
+------+
| id   |
+------+
|    2 |
|    1 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

mysql> select distinct id,name  from user1;
+------+--------+
| id   | name   |
+------+--------+
|    2 | 小O    |
|    1 | 张三   |
|    2 | 大O    |
|    3 | 小四   |
|    4 | 小小   |
+------+--------+
5 rows in set (0.00 sec)

5)查询结果参与计算
如:select 字段1+字段2 from 表名;select 字段1*個定数值 from 表名;等。

mysql> select * from user;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  1 | 张三   |   80 |      70 |
|  2 | 大O    |  140 |      50 |
|  3 | 小四   |  150 |      30 |
|  4 | 小小   |   40 |     120 |
+----+--------+------+---------+
4 rows in set (0.00 sec)

mysql> select *,math+english 总分 from user;
+----+--------+------+---------+--------+
| id | name   | math | english | 总分   |
+----+--------+------+---------+--------+
|  1 | 张三   |   80 |      70 |    150 |
|  2 | 大O    |  140 |      50 |    190 |
|  3 | 小四   |  150 |      30 |    180 |
|  4 | 小小   |   40 |     120 |    160 |
+----+--------+------+---------+--------+
4 rows in set (0.01 sec)

三、简单条件查询

1)条件查询
语法:select [字段]... from 表名 where 条件;

运算符 说明
> 大于
< 小于
<= 小于或等于
>= 大于或等于
= 等于
<>、!= 不等于
and 多个条件同时满足
or 多个条件中满足其中一个
between 满足指定范围
in 在指定值里面
not in 不在指定值里面
mysql> select * from user where math > 100;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  2 | 大O    |  140 |      50 |
|  3 | 小四   |  150 |      30 |
+----+--------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from user where english between 50 and 100;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  1 | 张三   |   80 |      70 |
|  2 | 大O    |  140 |      50 |
+----+--------+------+---------+
2 rows in set (0.00 sec)

2)模糊查询
语法:select [字段]... from 表名 like '通配符字符串';

MySQL通配符:
%:表示0个或者多个任意字符
_:表示一个任意字符

mysql> select * from user where name like "%小%";
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  3 | 小四   |  150 |      30 |
|  4 | 小小   |   40 |     120 |
+----+--------+------+---------+
2 rows in set (0.00 sec)

四、排序与分页

1)简单排序
语法:select [字段]... from 表名 order by 字段 [asc|desc],...
默认排序方式为:asc(升序)

mysql> select * from user order by math;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  4 | 小小   |   40 |     120 |
|  1 | 张三   |   80 |      70 |
|  2 | 大O    |  140 |      50 |
|  3 | 小四   |  150 |      30 |
+----+--------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from user order by math desc;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  3 | 小四   |  150 |      30 |
|  2 | 大O    |  140 |      50 |
|  1 | 张三   |   80 |      70 |
|  4 | 小小   |   40 |     120 |
+----+--------+------+---------+
4 rows in set (0.00 sec)

2)分页
语法:select [字段]... from 表名 limit offset,length;select [字段]... from 表名 limit length;
offset 是指偏移量,不指定的话默认为0。
length 需要显示的总记录数

mysql> select * from user;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  1 | 张三   |   80 |      70 |
|  2 | 大O    |  140 |      50 |
|  3 | 小四   |  150 |      30 |
|  4 | 小小   |   40 |     120 |
+----+--------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from user limit 1,2;
+----+--------+------+---------+
| id | name   | math | english |
+----+--------+------+---------+
|  2 | 大O    |  140 |      50 |
|  3 | 小四   |  150 |      30 |
+----+--------+------+---------+
2 rows in set (0.00 sec)

五、参考

8.0官方文档:https://dev.mysql.com/doc/refman/8.0/en/

你可能感兴趣的:(MySQL基础04-数据简单查询(DQL语句))