首页 > 编程笔记 > MySQL笔记 阅读:61

SQL SELECT语句的用法(附带实例)

通过 SQL 的 SELECT 语句可以查询数据表中的数据,而且查询的方式可以分为好几种类型,接下来介绍几种最基础也最常用的查询语句。

SELECT 语句的语法格式为:
SELECT {*|列名1, 列名2, 列名n} FROM 表名1 [,表名2,表名n]
[WHERE 条件表达式]
...;
各字段的含义如下:
【实例 1】查询表中所有字段的数据和指定字段的数据。
##查看表的结构
MariaDB [test01]> desc stu_score;
+-------------+------------+------+------+---------+-------+
| Field       | Type       | Null | Key  | Default | Extra |
+-------------+------------+------+------+---------+-------+
| stu_id      | int(11)    | NO   |      | NULL    |       |
| name        | varchar(20)| NO   | PRI  | NULL    |       |
| english     | char(3)    | NO   |      | NULL    |       |
| mathematics | char(3)    | YES  |      | NULL    |       |
| geography   | char(3)    | YES  |      | NULL    |       |
+-------------+------------+------+------+---------+-------+
5 rows in set (0.001 sec)

##查询表中所有字段中的数据
MariaDB [test01]> select * from stu_score;
+--------+--------+---------+------------+------------+
| stu_id | name   | English | mathematics | geography |
+--------+--------+---------+------------+------------+
| 2      | 小刘   | 67      | 58         | 74         |
| 1      | 小孙   | 56      | 44         | 96         |
| 3      | 小崔   | 78      | 76         | 48         |
| 4      | 小狗   | 76      | 39         | 69         |
| 5      | 小猫   | 88      | NULL       | NULL       |
+--------+--------+---------+------------+------------+
5 rows in set (0.114 sec)

##查询表中指定字段中的数据
MariaDB [test01]> select name,english from stu_score;
+--------+---------+
| name   | English |
+--------+---------+
| 小刘   | 67      |
| 小孙   | 56      |
| 小崔   | 78      |
| 小狗   | 76      |
| 小猫   | 88      |
+--------+---------+
5 rows in set (0.000 sec)

接下来的查询语句稍微复杂一些,因为会加上 WHERE 关键字,加上 WHERE 关键字可以实现筛选数据(也就是满足查询条件进行查询)的功能。

查询条件的类型有很多种,常见的包括:
通过以上查询条件大家应该能够感受到 SELECT 查询语句的多样性和复杂性。但在实际应用中,很多查询条件只有在特定的需求下才会用到,平常只需要通过简单的几条 SQL 语句就能查看指定字段中的数据。

【实例 2】实践各种查询条件。
[root@noylinux ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.5-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
+--------------------+
5 rows in set (0.087 sec)

MariaDB [(none)]> use test01;
Database changed

MariaDB [test01]> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| stu_score        |
+------------------+
1 row in set (0.001 sec)

MariaDB [test01]> select * from stu_score;
+--------+--------+--------+------------+-------------+
| stu_id | name   | English | mathematics | geography |
+--------+--------+--------+------------+-------------+
| 2      | 小刘   | 67     | 58         | 74          |
| 1      | 小孙   | 56     | 44         | 96          |
| 3      | 小崔   | 78     | 76         | 48          |
| 4      | 小狗   | 76     | 39         | 69          |
| 5      | 小猫   | 88     | NULL       | NULL        |
+--------+--------+--------+------------+-------------+
5 rows in set (0.105 sec)

根据表中数据的特性演示各种筛选条件:
##筛选出在 mathematics 字段中值大于 50 的数据记录(运算符)
MariaDB [test01]> select * from stu_score where mathematics>50;
+--------+--------+--------+------------+------------+
| stu_id | name   | english| mathematics| geography  |
+--------+--------+--------+------------+------------+
| 2      | 小刘   | 67     | 58         | 74         |
| 3      | 小崔   | 78     | 76         | 48         |
+--------+--------+--------+------------+------------+
2 rows in set (0.001 sec)

##筛选出在 english 字段中值小于 70 的数据记录(运算符)
MariaDB [test01]> select * from stu_score where english<70;
+--------+--------+--------+------------+------------+
| stu_id | name   | english| mathematics| geography  |
+--------+--------+--------+------------+------------+
| 2      | 小刘   | 67     | 58         | 74         |
| 1      | 小孙   | 56     | 44         | 96         |
+--------+--------+--------+------------+------------+
2 rows in set (0.001 sec)

##筛选出在 geography 字段中值在 70 至 100 之间的数据记录(区间/范围)
MariaDB [test01]> select * from stu_score where geography between 70 and 100;
+--------+--------+--------+------------+------------+
| stu_id | name   |english | mathematics| geography  |
+--------+--------+--------+------------+------------+
| 2      | 小刘   | 67     | 58         | 74         |
| 1      | 小孙   | 56     | 44         | 96         |
+--------+--------+--------+------------+------------+
2 rows in set (0.002 sec)

##筛选出在 name 字段中值包含小孙、小猫和小崔的数据记录(确定集合)
MariaDB [test01]> select * from stu_score where name in('小孙','小猫','小崔');
+--------+--------+---------+------------+------------+
| stu_id | name   | english | mathematics| geography  |
+--------+--------+---------+------------+------------+
| 1      | 小孙   | 56      | 44         | 96         |
| 3      | 小崔   | 78      | 76         | 48         |
| 5      | 小猫   | 88      | NULL       | NULL       |
+--------+--------+---------+------------+------------+
3 rows in set (0.000 sec)

##筛选出在 mathematics 字段中值不为空的数据记录(是否为空值)
MariaDB [test01]> select * from stu_score where mathematics is not null;
+--------+--------+---------+------------+------------+
| stu_id | name   | english | mathematics| geography |
+--------+--------+---------+------------+------------+
| 2      | 小刘   | 67      | 58         | 74         |
| 1      | 小孙   | 56      | 44         | 96         |
| 3      | 小崔   | 78      | 76         | 48         |
| 4      | 小狗   | 76      | 39         | 69         |
+--------+--------+---------+------------+------------+
4 rows in set (0.000 sec)

##筛选出在 english 字段中值以 7 开头的数据记录(模糊查询)
MariaDB [test01]> select * from stu_score where english like '7%';
+--------+--------+---------+-------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+---------+-------------+------------+
| 3      | 小崔   | 78      | 76          | 48         |
+--------+--------+---------+-------------+------------+
1 row in set (0.000 sec)

##筛选出在 name 字段中值以小狗结尾的数据记录(模糊查询)
MariaDB [test01]> select * from stu_score where name like '%狗';
+--------+--------+---------+-------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+---------+-------------+------------+
| 4      | 小狗   | 76      | 39          | 69         |
+--------+--------+---------+-------------+------------+
1 row in set (0.000 sec)

##筛选出在 geography 字段中值大于 50 并且小于 70 的数据记录(多条件查询)
MariaDB [test01]> select * from stu_score where geography>50 and geography<70;
+--------+--------+---------+-------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+---------+-------------+------------+
| 4      | 小狗   | 76      | 39          | 69         |
+--------+--------+---------+-------------+------------+
1 row in set (0.000 sec)

##筛选出在 english 字段中值大于 70 并且在 geography 字段中值大于 50 的数据记录(多条件查询)
MariaDB [test01]> select * from stu_score where english>70 and geography>50;
+--------+--------+--------+--------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+--------+--------------+------------+
| 4      | 小狗   | 76     | 39           | 69         |
+--------+--------+--------+--------------+------------+
1 row in set (0.001 sec)
一定要记住,查询条件表达式并不是固定的,而是变化的,查询条件与查询条件之间可以随意组合,不同的组合会展现出不同的查询效果,只要逻辑上能说得通就可以随机应变。

相关文章