5、使用各种条件进行提取
之前都是在使用
select * from tableName;语句来对表中的内容进行信息的提取操作;但是,这样的提取方式是对数据库中的所有的内容进行提取,我们现在需要做的是精准的定位。SELECT是SQL的基础。明确各个数据要以怎样的形式提取之后,自由操作SELECT是我们要达到的目标。
5.1、改变表的显示顺序(在不改变表的前提下)
5.1.1、改变显示的顺序:
在指定了列名的情况下,列会按照指定的顺序来进行显示。当指定多个列时,可以使用,来分隔列名。
select sales,empid from tb;
同一个列可以显示多次。例如执行命令SELECT sales,empid,sales,empid,sales, empid FROM tb;,列 sales 和列 empid 就会各显示 3 次。
5.1.2、改变显示的列名(起别名):
对应的使用的语法:
SELECT 列名 AS 别名 FROM 表名 ;
实际的使用案例:
SELECT empid AS 员工号,sales AS 销售额 FROM tb;
5.2、显示计算列值或者处理后的字符串
5.2.1、列相关的计算操作:
两种计算方式:
- 直接进行数值上的计算。
- 在同一条记录的各列值之间进行的计算。
我们可以使用列中的数据自由地进行乘法和除法等四则运算,然后将其结果作为列然后将其显示:
可以使用的计算操作有:
| 运算符 | 使用示例 | 含义 |
|---|---|---|
+ | a + b | a 加上 b |
- | a - b | a 减去 b |
* | a * b | a 乘以 b |
/ | a / b | a 除以 b |
DIV | a DIV b | a 除以 b(结果取整) |
%、MOD | a % b | a 除以 b取余 |
对应的使用实例如下:
MySQL [testdb1]> select * from table2;
+------+------+------+------+
| name | age | des_ | age1 |
+------+------+------+------+
| a101 | 1 | 100 | 1 |
| a102 | 2 | 200 | 2 |
| a103 | 3 | 300 | 3 |
+------+------+------+------+
3 rows in set (0.000 sec)
MySQL [testdb1]> select age * 10000 from table2;
+-------------+
| age * 10000 |
+-------------+
| 10000 |
| 20000 |
| 30000 |
+-------------+
3 rows in set (0.000 sec)
MySQL [testdb1]> select age * 10000 as age乘100 from table2;
+-----------+
| age乘100 |
+-----------+
| 10000 |
| 20000 |
| 30000 |
+-----------+
3 rows in set (0.000 sec)
MySQL [testdb1]> select age + age1 as 和 from table2;
+------+
| 和 |
+------+
| 2 |
| 4 |
| 6 |
+------+
3 rows in set (0.000 sec)
MySQL [testdb1]>
【注:】
另外,如果是“销售额 ( 元 )”这种包含半角括号的情况,就需要使用 " " 将整个内容括起来,即“" 销售额 ( 元 )"”,否则就会发生错误。
5.2.2、使用函数来进行计算:
MySQL [(none)]> use testdb1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [testdb1]> show tables;
+-------------------+
| Tables_in_testdb1 |
+-------------------+
| table1 |
| table2 |
| table3 |
+-------------------+
3 rows in set (0.001 sec)
MySQL [testdb1]> select avg(age) from table2;
+----------+
| avg(age) |
+----------+
| 2.0000 |
+----------+
可以使用的函数:
avg(),sum(),count()分别用来算均值,总和和计数。
5.2.3、使用select来进行信息的查询:
可以使用select查询到的信息有:
-
PI():可以显示一些和数据库无关的计算操作;MySQL [testdb1]> select age * age * PI() from table2; +------------------+ | age * age * PI() | +------------------+ | 3.141593 | | 12.566371 | | 28.274334 | +------------------+ 3 rows in set (0.000 sec) MySQL [testdb1]> exit Bye -
Version():显示当前数据库的版本 -
Database():显示当前使用的数据库 -
user():显示当前的用户 -
charset('这个自负'):显示由参数指定的字符的字符编码MySQL [(none)]> select charset('哈哈哈'); +----------------------+ | charset('哈哈哈') | +----------------------+ | utf8mb3 | +----------------------+ 1 row in set (0.000 sec) MySQL [(none)]> select charset('哈哈哈') as 字符编码; +--------------+ | 字符编码 | +--------------+ | utf8mb3 | +--------------+ 1 row in set (0.000 sec) MySQL [(none)]>
5.2.4、连接字符串:
MySQL 中还有很多用于处理字符串的函数。例如在连接字符串的时候,可以使用 CONCAT 函
数。CONCAT 中指定的字符串列需要使用“,”进行分隔。比如在连接列 a、b、c 的字符时,就需
要写成 CONCAT(a,b,c)。该函数还可以直接指定字符串。
对应的使用方式如下:
MySQL [testdb1]> select * from table2;
+------+------+------+------+
| name | age | des_ | age1 |
+------+------+------+------+
| a101 | 1 | 100 | 1 |
| a102 | 2 | 200 | 2 |
| a103 | 3 | 300 | 3 |
+------+------+------+------+
3 rows in set (0.000 sec)
MySQL [testdb1]> select concat(name,age,age1) from table2;
+-----------------------+
| concat(name,age,age1) |
+-----------------------+
| a10111 |
| a10222 |
| a10333 |
+-----------------------+
3 rows in set (0.000 sec)
CONCAT中的内容可以是常量。
字符串操作中的常用的函数:
从右取出:right(listName,n),命令用于显示列 listName 最右边的 n 个字符。
从左取出:left(listName,n),命令用于显示列listName 最左边的 n 个字符。
从第x个字符开始截取y个字符:substring(listName,x,y)。
repeat(字符,listName):下面的命令用于重复显示字符,其重复次数为列 listName 的值
reverse(listName):这个指令用于把对应的列(listName)中的内容的字符串逆向输出。
5.2.5、对显示进行条件筛选
针对数量上的筛选和内容上的筛选。
5.2.5.1、针对数量上的筛选:
limit关键字的使用,来限制要显示的记录数。
使用如下的语句来限制显示的数量:
SELECT 列名 FROM 表名 LIMIT 显示的记录数 ;
5.2.5.2、对记录上进行筛选:
1)精准查找:
使用关键字:where
我们可以使用 WHERE 设置条件并取出与条件相匹配的记录。能否顺利提取记录可以说取决于如何使用 WHERE。
使用where关键字的基本逻辑:
SELECT 列名 FROM 表名 WHERE 条件 ;
上述的语句,是用来显示满足条件的所有的记录。
2)模糊查找
使用关键字:like
比如既想提取 A101,又想提取 A102,这时就可以执行模糊查询。如果想使用“包含 ×× 字
符”这种含糊不清的条件执行查询,就需要用到 LIKE。
使用LIKE和where的逻辑基本是一样的,但是在条件的编写中需要加上一些正则表达式的内容。
select 列名 from 表名 where listName like "re_string";
LIKE 可以把包含某字符串的所有内容当成查询对象。在这种情况下,需要用到%和_等通配符。当设置条件时,%代表任意字符串,_代表任意一个字符。
提取不包含指定字符串的记录
另外,提取不包含某字符串的记录时需要使用 NOT LIKE 命令。
3)判断条件的查找:
| 比较运算符 | 含义 |
|---|---|
| = | 等于 |
| > | 大于 |
| >= | 大于等于 |
| < | 小于 |
| <= | 小于等于 |
| <> | 不等于 |
| ○ IN × | ○在 × 列表中,x=(m, n) |
| ○ NOT IN × | ○不在 × 列表中,x=(m, n) |
| ○ BETWEEN × AND ×× | ○在 × 到 ×× 之间 |
| ○ NOT BETWEEN × AND ×× | ○不在 × 到 ×× 之间 |
上述的方法都是针对数值条件的设置方法。下面所示的就是字符串的相关的设置方法。这里好像只能使用=来进行字符串的设置。如下:
select * from TableName where listName="string";
进行多个条件的测试:
条件的逻辑关系
-
AND -
OR -
使用多个
AND和OR:当AND和OR同时使用的时候,会优先处理AND。若想修改优先级的操作需要使用括号。 -
使用
CASE WHEN:根据条件改变输入值(这个是改变输出的值。)。select case when condition1 then xxxx when condition2 then xxxx else xxxx end from TableName;
4)对NULL的处理:
需要注意的是,当提取值为 NULL 的记录时,即使使用 WHERE age = NULL,也无法提取出相应的记录。
筛选列值为NULL的记录:
对应的IS NULL关键字的使用:
select * from tableName where ListName IS NULL
筛选列值不为NULL的记录
对应的使用的关键字为:IS NOT NULL的使用:
select * from tableName where ListName IS NOT NULL;
5)显示一个列(隔绝重复选项):
select distinct ListName from TableName;
将显示TableName中的ListName列,并筛除所有的重复选项。
5.2.5.3、排序(不在where后面,只需要在table后面即可)
排序的三种方式:
- 按升序排序并显示:
order by ListName asc- 按降序排序并显示:
order by ListName DESC- 指定记录的显示范围:
select ListName from TableName limit 显示的记录数 offset 开始显示记录的移位数;
5.3、分组操作:
使用
group by来实现对应的分组操作;一定要记住处理对象是“同一组中的所有记录”。
5.3.1、简单的分组操作:
我们先观察一下group by的使用方法:
MySQL [users]> select * from user;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A103 | 17 | 5 |
| A101 | 300 | 5 |
| A102 | 205 | 6 |
| A104 | 93 | 5 |
| A103 | 12 | 6 |
| A107 | 87 | 6 |
+-------+-------+-------+
10 rows in set (0.000 sec)
MySQL [users]> select * from user group by empid;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A107 | 87 | 6 |
+-------+-------+-------+
5 rows in set (0.000 sec)
使用分组之后,所有的之前对于列的操作,都是在分组的场景下使用的。下面的count()和sum()和avg()三个函数都是针对所分的组而言的。可以,如下所示:
MySQL [users]> select
-> empid as 工号,
-> sum(sales) as 总和,
-> avg(sales) as 均值,
-> count(sales) as 数量
-> from user group by empid;
+--------+--------+----------+--------+
| 工号 | 总和 | 均值 | 数量 |
+--------+--------+----------+--------+
| A103 | 130 | 43.3333 | 3 |
| A102 | 259 | 129.5000 | 2 |
| A104 | 274 | 137.0000 | 2 |
| A101 | 484 | 242.0000 | 2 |
| A107 | 87 | 87.0000 | 1 |
+--------+--------+----------+--------+
我们可以看到如上所示的结果,发现是按照empid的分组来进行运算的。
可以使用列的序号,来当作列名来进行分组的操作:
我们看如下的操作,来理解上述的话。
MySQL [users]> select * from user group by 1;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A107 | 87 | 6 |
+-------+-------+-------+
5 rows in set (0.000 sec)
MySQL [users]> select * from user group by empid;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A107 | 87 | 6 |
+-------+-------+-------+
5 rows in set (0.000 sec)
MySQL [users]>
group by的序列号玩法,多个序列号之间执行的是与操作:
MySQL [users]> select * from user group by 1,2;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A103 | 17 | 5 |
| A101 | 300 | 5 |
| A102 | 205 | 6 |
| A104 | 93 | 5 |
| A103 | 12 | 6 |
| A107 | 87 | 6 |
+-------+-------+-------+
10 rows in set (0.000 sec)
MySQL [users]> select * from user group by 1,2,3;
+-------+-------+-------+
| empid | sales | month |
+-------+-------+-------+
| A103 | 101 | 4 |
| A102 | 54 | 5 |
| A104 | 181 | 4 |
| A101 | 184 | 4 |
| A103 | 17 | 5 |
| A101 | 300 | 5 |
| A102 | 205 | 6 |
| A104 | 93 | 5 |
| A103 | 12 | 6 |
| A107 | 87 | 6 |
| A107 | 87 | NULL |
+-------+-------+-------+
11 rows in set (0.000 sec)
MySQL [users]>
5.3.2、条件分组显示:
5.3.2.1、分组之后再筛选:
having关键字:可以针对分组完的数据,在执行筛选的操作。
SELECT 统计列 FROM 表名 GROUP BY 分组列 HAVING 条件
使用的案例如下:
MySQL [users]> select empid,sum(sales)
-> from user
-> group by empid
-> having sum(sales)>=200;
+-------+------------+
| empid | sum(sales) |
+-------+------------+
| A102 | 259 |
| A104 | 274 |
| A101 | 484 |
+-------+------------+
3 rows in set (0.000 sec)
MySQL [users]> select empid,sum(sales) from user group by empid;
+-------+------------+
| empid | sum(sales) |
+-------+------------+
| A103 | 130 |
| A102 | 259 |
| A104 | 274 |
| A101 | 484 |
| A107 | 174 |
+-------+------------+
5 rows in set (0.000 sec)
MySQL [users]>
5.3.2.2、筛选之后在进行分组的操作:
select
empid,sum(sales)
from user
where sales>=50
group by empid;
最后的结果显示为:
MySQL [users]> select
-> empid,sum(sales)
-> from user
-> where sales>=50
-> group by empid;
+-------+------------+
| empid | sum(sales) |
+-------+------------+
| A103 | 101 |
| A102 | 259 |
| A104 | 274 |
| A101 | 484 |
| A107 | 174 |
+-------+------------+
5 rows in set (0.000 sec)
5.3.2.3、分组之后再排序:
select
listname,function(listname)
from tableName
group by listName2
order by listName3;
针对之前的那个数据库,我们按照empid来进行分组。然后,按照分组的均值来进行进行排序。
对应的使用方法如下所示:
select
empid as 工号,
avg(empid) as 均值
from user
group by empid
order by avg(empid);
对应的使用结果为:
MySQL [users]> select
-> empid as 工号,
-> avg(sales) as 均值
-> from user
-> group by empid
-> order by avg(sales);
+--------+----------+
| 工号 | 均值 |
+--------+----------+
| A103 | 43.3333 |
| A107 | 87.0000 |
| A102 | 129.5000 |
| A104 | 137.0000 |
| A101 | 242.0000 |
+--------+----------+
5 rows in set (0.000 sec)
对应的,我们使用如下的方式也可以进行筛选:
MySQL [users]> select
empid as 工号,
avg(sales) as 均值
from user
group by 工号
order by 均值;
+--------+----------+
| 工号 | 均值 |
+--------+----------+
| A103 | 43.3333 |
| A107 | 87.0000 |
| A102 | 129.5000 |
| A104 | 137.0000 |
| A101 | 242.0000 |
+--------+----------+
5 rows in set (0.000 sec)
5.4、总结:
对应的提取,分组,排序的顺序;
1) where -> 2) group by -> 3) having -> 4) order by
2万+



被折叠的 条评论
为什么被折叠?



