SQL SELECT语句的用法(附带实例)
通过 SQL 的 SELECT 语句可以查询数据表中的数据,而且查询的方式可以分为好几种类型,接下来介绍几种最基础也最常用的查询语句。
SELECT 语句的语法格式为:
【实例 1】查询表中所有字段的数据和指定字段的数据。
接下来的查询语句稍微复杂一些,因为会加上 WHERE 关键字,加上 WHERE 关键字可以实现筛选数据(也就是满足查询条件进行查询)的功能。
查询条件的类型有很多种,常见的包括:
通过以上查询条件大家应该能够感受到 SELECT 查询语句的多样性和复杂性。但在实际应用中,很多查询条件只有在特定的需求下才会用到,平常只需要通过简单的几条 SQL 语句就能查看指定字段中的数据。
【实例 2】实践各种查询条件。
根据表中数据的特性演示各种筛选条件:
SELECT 语句的语法格式为:
SELECT {*|列名1, 列名2, 列名n} FROM 表名1 [,表名2,表名n] [WHERE 条件表达式] ...;各字段的含义如下:
- {*|列名1, 列名2, 列名n}:要查询的字段名称。星号是通配符,表示查询表中所有的字段;列名表示要查询指定的字段,这里可以写多个字段,但是字段名与字段名之间要用英文逗号进行分隔。这两种方式二选一!
- 表名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 关键字可以实现筛选数据(也就是满足查询条件进行查询)的功能。
查询条件的类型有很多种,常见的包括:
- 常见运算符:=、>、<、>=、<=、!=等。
- 区间/范围:BETWEEN...AND...,表示在某一范围内;NOT BETWEEN...AND...,表示不在某一范围内。
- 确定集合:IN,表示包含某元素的数据;NOT IN,表示不包含某元素的数据。
- 是否为空值:IS NULL,判断数据为空值;IS NOT NULL,判断数据不为空值。
- 模糊查询:LINK‘字符串’,表示匹配条件的字符串。支持百分号“%”和下划线“_”通配符。百分号“%”表示任何多个任意字符;下划线“_”表示单个任意字符。NOT LINK‘字符串’,表示不匹配条件的字符串。
- 多条件查询:AND 表示当记录满足所有查询条件时,才会被查询出来;OR 表示当记录满足任意一个查询条件时,就会被查询出来;XOR 表示当记录满足其中一个条件,并且不满足另一个条件时,才会被查询出来。
通过以上查询条件大家应该能够感受到 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)一定要记住,查询条件表达式并不是固定的,而是变化的,查询条件与查询条件之间可以随意组合,不同的组合会展现出不同的查询效果,只要逻辑上能说得通就可以随机应变。