MySQL数据查询操作(DQL)

数据准备工作:

 1 CREATE TABLE student(
 2     sid INT PRIMARY KEY AUTO_INCREMENT,
 3     `name` VARCHAR(10),
 4     age INT,
 5     city VARCHAR(10)
 6 );
 7 
 8 INSERT INTO student VALUES (NULL,"张三",20,"广州");
 9 INSERT INTO student VALUES (NULL,"李四",22,"深圳");
10 INSERT INTO student VALUES (NULL,"王五",17,"佛山");
11 INSERT INTO student VALUES (NULL,"赵六",24,"广州");
12 INSERT INTO student VALUES (NULL,"孙七",20,NULL);

一、简单查询

语法:select [distinct] * | 列名1,列名2 from 表;

1.1 查询所有学生

查询指令:select * from student;

mysql> select * from student;
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   1 | 张三   |   20 | 广州   |
|   2 | 李四   |   22 | 深圳   |
|   3 | 王五   |   17 | 佛山   |
|   4 | 赵六   |   24 | 广州   |
|   5 | 孙七   |   20 | NULL   |
+-----+--------+------+--------+
5 rows in set (0.00 sec)

1.2 查询学生姓名和年龄

查询指令:select name,age from student;

mysql> select name,age from student;
+--------+------+
| name   | age  |
+--------+------+
| 张三   |   20 |
| 李四   |   22 |
| 王五   |   17 |
| 赵六   |   24 |
| 孙七   |   20 |
+--------+------+
5 rows in set (0.00 sec)

1.3 运算查询:将所有学生的年龄+1进行显示

查询指令:select name,age+1 from student;

mysql> select name,age+1 from student;
+--------+-------+
| name   | age+1 |
+--------+-------+
| 张三   |    21 |
| 李四   |    23 |
| 王五   |    18 |
| 赵六   |    25 |
| 孙七   |    21 |
+--------+-------+
5 rows in set (0.00 sec)

 1.4 别名查询:将列名name,age分别用中文显示

查询指令:select name as 姓名,age as 年龄 from student;

mysql> select name as 姓名,age as 年龄 from student;
+--------+--------+
| 姓名   | 年龄   |
+--------+--------+
| 张三   |     20 |
| 李四   |     22 |
| 王五   |     17 |
| 赵六   |     24 |
| 孙七   |     20 |
+--------+--------+
5 rows in set (0.00 sec)

二、条件查询

语法:select [distinct] * | 列名1,列名2 from 表 where 条件;

 

2.1 查询年龄大于20的学生

查询指令:select * from student where age > 20;

mysql> select * from student where age > 20;
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   2 | 李四   |   22 | 深圳   |
|   4 | 赵六   |   24 | 广州   |
+-----+--------+------+--------+
2 rows in set (0.00 sec)

2.2 查询年龄是18到22的学生

查询指令:select * from student where age between 18 and 22;

mysql> select * from student where age between 18 and 22;
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   1 | 张三   |   20 | 广州   |
|   2 | 李四   |   22 | 深圳   |
|   5 | 孙七   |   20 | NULL   |
+-----+--------+------+--------+
3 rows in set (0.00 sec)

2.3 查询地址是广州或深圳的学生

查询指令:select * from student where city in ("广州","深圳");

mysql> select * from student where city in ("广州","深圳");
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   1 | 张三   |   20 | 广州   |
|   2 | 李四   |   22 | 深圳   |
|   4 | 赵六   |   24 | 广州   |
+-----+--------+------+--------+
3 rows in set (0.00 sec)

2.4 查询姓孙的学生

查询指令:select * from student where name like "孙%";

mysql> select * from student where name like "孙%";
+-----+--------+------+------+
| sid | name   | age  | city |
+-----+--------+------+------+
|   5 | 孙七   |   20 | NULL |
+-----+--------+------+------+
1 row in set (0.00 sec)

2.5 查询地址不为空的学生

查询指令:select * from student where city IS NOT NULL;

mysql> select * from student where city IS NOT NULL;
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   1 | 张三   |   20 | 广州   |
|   2 | 李四   |   22 | 深圳   |
|   3 | 王五   |   17 | 佛山   |
|   4 | 赵六   |   24 | 广州   |
+-----+--------+------+--------+
4 rows in set (0.00 sec)

2.6 查询地址为广州或深圳,且年龄大于20的学生

查询指令:select * from student where city in ("广州","深圳") and age > 20;

mysql> select * from student where city in ("广州","深圳") and age > 20;
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   2 | 李四   |   22 | 深圳   |
|   4 | 赵六   |   24 | 广州   |
+-----+--------+------+--------+
2 rows in set (0.00 sec)

2.7 查询地址不是广州或深圳的学生

查询指令:select * from student where city not in ("广州","深圳");

mysql> select * from student where city not in ("广州","深圳");
+-----+--------+------+--------+
| sid | name   | age  | city   |
+-----+--------+------+--------+
|   3 | 王五   |   17 | 佛山   |
+-----+--------+------+--------+
1 row in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/heqiuyong/p/9484846.html