四、MySQL的数据查询

本文详细介绍了MySQL的基本查询语句,包括单表查询的各种操作如查询指定字段、条件查询、排序和分组,以及集合函数的使用。还深入讲解了连接查询(内连接、外连接和复合条件连接)和子查询的多种形式,如any/some、all、exists和in关键字的使用。此外,提到了正则表达式在查询中的应用和MySQL8.0的新特性,如groupby不再隐式排序和通用表表达式。
摘要由CSDN通过智能技术生成

一、基本查询语句

二、单表查询语句

1、查询所有字段

2、查询指定字段

3、查询指定纪录

4、带 IN 关键字的查询

5、带 between and 的范围查询

6、带 like 的字符匹配查询

7、查询空值

8、带 and 的多条件查询

9、带 or 的多条件查询

10、查询结果不重复

11、对查询结果排序

12、分组查询

13、使用 limit 限制查询结果的数量

三、使用集合函数查询

1、count()函数

2、sum()函数

3、avg()函数

4、max()函数

5、min()函数

四、连接查询

1、内连接查询

2、外连接查询

3、复合条件连接查询

五、子查询

1、带 any、some 关键字的子查询

2、带 all 关键字的子查询

3、带 exists 关键字的子查询

4、带 in 关键字的子查询

5、带比较运算符的子查询

六、合并查询结果

七、为表和字段取别名

1、为表取别名

2、为字段取别名

八、使用正则表达式查询

1、查询以特定字符或字符串开头的记录

2、查询以特定字符或字符串结尾的记录

3、用符号“.”来替代字符串中的任意一个字符

4、使用“*”和“+”来匹配多个字符

5、匹配指定字符串

6、匹配指定字符中的任意一个

7、匹配指定字符以外的字符

8、使用{n,}或{n,m}来指定字符串连续出现的次数

九、MySQL8.0新特性

1、group by 不再隐式排序

2、通用表表达式


一、基本查询语句

select
        { * | <字段列表>}
        [
              from  <表1>,<表2>......
              [ where  <表达式> ]
              [group  by  <group  by  definition>]
              [having  <expression>  [{<operator>  <expression>} ... ]]
              [order  by <order  by definition>]
              [limit  [<offset>,]   <row  count> ]
        ]
  • { * | <字段列表>} 包含星号通配符和字段列表,表示查询的字段,其中,字段列表至少包含一个字段名称,如果要查询多个字段,多个字段之间用逗号隔开,最后一个字段不加逗号。
  • from <表1>,<表2>…,表1和表2表示查询数据的来源,可以是单个或者多个。
  • where 子句是可选项,如果选择该项,将限定查询行必须满足的查询条件。
  • group by <字段>,该子句告诉MySQL如何显示查询出来的数据,并按照指定的字段分组。
  • order by <字段>,该子句告诉mysql按什么样的顺序显示查询出来的数据,可以进行的排序有升序(ASC)、降序(DESC)。
  • limit,该子句告诉mysql每次显示查询出来的数据条数。

二、单表查询语句

1、查询所有字段

select  *  from  表名;

2、查询指定字段

select  列名  from  表名;

3、查询指定纪录

select  字段名1,字段名2,... ,字段名n
from   表名
where   查询条件

where 条件判断符

操作符说明
=等于
<>,!=不等于
<小于
<=小于等于
>大于
>=大于等于
between位于两值之间
#案例
#1、查询价格为10.2元的水果名称
select  f_name,f_price  
from  fruits
where   f_price = 10.2;

#2、查询名称为“apple”的水果的价格
select  f_name,f_price 
from  fruits
where  f_name  = ''apple;

#3、从fruits表中获取价格低于10的水果名称,即f_price小于10的水果信息被返回
select  f_name,f_price
from  fruits
where  f_price < 10;

4、带 IN 关键字的查询

#1、查询s_id为101和102的记录
select  s_id,f_name,f_price
from  fruits
where  s_id  in  (101,102)
order by  f_name

#2、查询s_id不等于101和102的记录
select  s_id,f_name,f_price
from  fruits
where  s_id  not  in (101,102)
order by  f_name

5、带 between and 的范围查询

#1、查询水果价格在2到10之间的
select  f_name,f_price  
from  fruits  
where  f_price  between 2  and  10;

6、带 like 的字符匹配查询

1)百分号通配符‘%’,匹配任意长度的字符,甚至包括零字符

#1、查找所有以 ‘b’ 字母开头的水果
select  f_id,f_name
from  fruits
where  f_name  like  'b%'

#2、在fruits表中,查询f_name中包含字母 ‘g’ 的记录
select  f_id,f_name
from  fruits
where  f_name  like  '%g%'

2)下划线通配符 ‘_’,一次只能匹配任意一个字符

#1、在fruits表中,查询以字母 ‘y’ 结尾,且‘y’ 前面只有4个字母的记录
select  f_id,f_name  from  fruits  where  f_name  like '---y'

7、查询空值

#1、查询customers表中c_email为空的记录的c_id、c_name和c_email字段
select c_id,c_name,c_email
from  customers
where  c_email is  null;

#1、查询customers表中c_email不为空的记录的c_id、c_name和c_email字段
select c_id,c_name,c_email
from  customers
where  c_email   is  not  null;

8、带 and 的多条件查询

#1、在fruits表中查询s_id=101或者102,且f_price大于等于5、f_name='apple'的水果价格和名称
select  f_id,f_price,f_name
from  fruits
where s_id in ('101','102')  and  f_price >= 5  and  f_name = 'apple'

9、带 or 的多条件查询

#1、查询s_id=101 或者 s_id =102 的水果供应商的f_price和f_name
select  s_id,f_name,f_price
from   fruits
where  s_id  in  (101,102);

#2、查询s_id=101 或者 s_id =102 的水果供应商的f_price和f_name
select  s_id,f_name,f_price
from   fruits
where  s_id=101 or  s_id=102;

10、查询结果不重复

selsect  distinct  字段名  from  表名

11、对查询结果排序

1)单列排序

#1、查询fruits表中f_name字段值,并对其进行排序
select  f_name  
from  fruits 
order  by  f_name

2)多列排序

#1、查询fruits表中的f_name和f_price字段,先按f_name 排序,在按f_price排序
select  f_name,f_price  
from  fruits  
order by  f_name,f_price

3)指定排序方向

  • 默认升序排列
#1、查询fruits表中的f_name和f_price 字段,对结果按f_price降序方式排序
select  f_name,f_price  
from  fruits  
order by f_price  desc;

#2、查询fruits表,先按f_price降序排列,再按f_name字段升序排列
select  f_price,f_name
from  fruits
order  by  f_price  desc,  f_name;

12、分组查询

[ group  by  字段 ]   [having  <条件表达式>]

1)创建分组

  1. count()函数计数函数
  2. group_concat() , 将每个分组中各个字段的值显示出来
#1、根据s_id 对 fruits 表中的数据进行分组
select  s_id,count(*)  as  total
from  fruits
group  by  s_id;

#2、根据s_id 对fruits表中的数据进行分组,将每个供应商的水果名称显示出来
select  s_id,group_concat(f_name)   as  names
from  fruits
group by  s_id;

2)使用 having 过滤分组

group by 可以和 having 一起限定显示记录所需满足的条件,只有满足条件的分组才会被显示

#1、根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息
select  s_id,group_concat(f_name)  as  names
from  fruits
group  by  s_id  having  count(f_name) > 1;

3)在group by 子句中使用 with rollup

使用with rollup关键字之后,在所有查询出的分组记录之后增加一条记录,该记录计算查询出的所有记录的总和,即统计记录数量

#1、根据s_id对fruits表中的数据进行分组,并显示记录数量
select  s_id,count(*)  as  total
from fruits
group by  s_id  with rollup;

4)多字段分组

使用 group by 可以对多个字段进行分组,group by 关键字后面跟需要分组的字段,mysql根据多字段的值来进行层次分组,分组层次从左到右,即先按第1个字段分组,然后再第1个字段值相同的记录中再根据第2个字段的值进行分组

#1、根据s_id和f_name字段对fruits表中的数据进行分组
select  *  from  fruits  group by  s_id,f_name;

5)group by 和 order by一起使用

#1、查询订单价格大于100的订单号和总订单价格,按总订单价格排序显示结果
select  o_num,sum(quantity  *  item_price)  as  ordertotal
from  orderitems
group by  o_num
having  sum(quantity  *  item_price)  >= 100
order by  ordertotal;

13、使用 limit 限制查询结果的数量

limit  [位置偏移量,]   行数
  • 位置偏移量,指示mysql从那一行开始显示,是一个可选参数,如果不指定就会从第一条开始显示
  • 行数,显示几行。
#1、显示fruits表查询结果的前4行
select  *  from  fruits  limit  4;

#2、在fruits表中,是要用limit子句,返回从第5个记录开始的行数长度为3的记录
select  *  from  fruits  limit  4,  3 ;

三、使用集合函数查询

函数作用
count()函数返回某列的平均值
sum()函数返回某列的行数
avg()函数返回某列的最大值
max()函数返回某列的最小值
min()函数返回某列的和

1、count()函数

  • count(*)函数:计算表中的总得行数,不管某列是否有数值或者空值
  • count(字段名)函数:计算指定列下的行数,计算时忽略空值的行

2、sum()函数

3、avg()函数

4、max()函数

5、min()函数

四、连接查询

1、内连接查询

内连接使用比较运算符进行表间某些列数据的比较操作,并列出这些表中与连接条件匹配的数据行,组合成新的记录,也就是说,在内连接的查询中,只有满足条件的记录才能出现在结果关系中。

select  suppliers.s_id,s_name,f_name,f_price 
from  fruits  inner  join  suppliers 
on  fruits.s_id = suppliers.s_id;

此处用 on ,on=where

#案例
#1、建立两个表
mysql> select * from a1;
+----+----------+
| id | name     |
+----+----------+
|  1 | liming   |
|  2 | xiaohong |
+----+----------+
2 rows in set (0.00 sec)

mysql> select * from a2;
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

#2、使用inner join语法进行内连接查询,a1内连接a2
mysql> select a1.id,name,location
    -> from a1 inner join a2
    -> on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)

#2、使用inner join语法进行内连接查询,a2内连接a1
mysql> select a1.id,name,location from a2 inner join a1 on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)

#3、a2.id 进行内连接
mysql> select a2.id,name,location from a2 inner join a1 on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)

mysql> select a2.id,name,location from a1 inner join a2 on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)


注意where 和 on 的用法

2、外连接查询

外连接查询多个表中相关联的行,内连接时,返回查询结果集合中仅是符合查询条件和连接条件的行。有时需要包括没有关联的行中的数据,即返回查询结果集合中不仅包含符合连接的条件,还包括左表(左外连接或左连接)、右表(右外连接或右连接)或两个边接表(全外连接)中的所有数据行。

  • left join (左连接):返回包括左表中的所有记录和右表中连接字段相等的记录
  • right jion (右连接):返回包括右表中的所有记录和左表中连接字段相等的记录
#建立新表
mysql> select * from a1;
+----+----------+
| id | name     |
+----+----------+
|  1 | liming   |
|  2 | xiaohong |
+----+----------+
2 rows in set (0.00 sec)

mysql> select * from a2;
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

1)左连接

#1、a1表左连接a2表
mysql> select  a1.id,name,location from a1 left  outer join  a2 on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)

#1、a2表左连接a1表
mysql> select  a1.id,name,location from a2 left  outer join  a1 on a1.id = a2.id;
+------+----------+-------------+
| id   | name     | location    |
+------+----------+-------------+
|    1 | liming   | limingjia   |
|    2 | xiaohong | xiaohongjia |
| NULL | NULL     | wanghanjia  |
+------+----------+-------------+
3 rows in set (0.00 sec)

2)右连接

#1、a1表右连接a2表
mysql> select  a1.id,name,location from a1 right  outer join  a2 on a1.id = a2.id;
+------+----------+-------------+
| id   | name     | location    |
+------+----------+-------------+
|    1 | liming   | limingjia   |
|    2 | xiaohong | xiaohongjia |
| NULL | NULL     | wanghanjia  |
+------+----------+-------------+
3 rows in set (0.00 sec)

#1、a2表右连接a1表
mysql> select  a1.id,name,location from a2 right  outer join  a1 on a1.id = a2.id;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  1 | liming   | limingjia   |
|  2 | xiaohong | xiaohongjia |
+----+----------+-------------+
2 rows in set (0.00 sec)

3、复合条件连接查询

#1、使用 and 
mysql> select a2.id,name,location from a1 inner join a2 on a1.id = a2.id and a2.id=1;
+----+--------+-----------+
| id | name   | location  |
+----+--------+-----------+
|  1 | liming | limingjia |
+----+--------+-----------+
1 row in set (0.00 sec)

#2、使用排序
mysql> select a2.id,name,location from a1 inner join a2 on a1.id = a2.id order by a1.id desc;
+----+----------+-------------+
| id | name     | location    |
+----+----------+-------------+
|  2 | xiaohong | xiaohongjia |
|  1 | liming   | limingjia   |
+----+----------+-------------+
2 rows in set (0.00 sec)

五、子查询

子查询也是嵌套查询

1、带 any、some 关键字的子查询

any 和 some 关键字是同义词,表示满足其中任一条件,他们允许创建一个表达式对子查询的返回值列表进行比较,只要满足内层子查询中的任何一个比较条件,就返回一个结果作为外层查询的条件

#1、建表
mysql> select * from tb11;
+------+
| num1 |
+------+
|    1 |
|    5 |
|   13 |
|   27 |
+------+
4 rows in set (0.00 sec)

mysql> select * from tb12;
+------+
| num2 |
+------+
|    6 |
|   14 |
|   11 |
|   20 |
+------+
4 rows in set (0.00 sec)

#2、返回tb12表的所有num2列,然后将tb11中的num1的值与之进行比较,只要大于num2的任何一个值,即为符合查询条件的结果
mysql> select num1 from tb11 where num1 > any(select num2 from tb12);
+------+
| num1 |
+------+
|   13 |
|   27 |
+------+
2 rows in set (0.00 sec)

2、带 all 关键字的子查询

#1、返回tb11表中比tb12表num2列所有值都大的值
mysql> select num1 from tb11 where num1 > all(select num2 from tb12);
+------+
| num1 |
+------+
|   27 |
+------+
1 row in set (0.00 sec)

3、带 exists 关键字的子查询

exists关键字后面的参数是一个任意的子查询,系统对子查询进行运算以判断它是否返回行,如果至少返回一行,那么exists的结果为true,此时外层查询语句将进行查询;如果子查询没有返回任何行,那么exists返回的结果是false,此时外层语句将不进行查询。

mysql> select num1 from tb11 where exists (select num2 from tb12);
+------+
| num1 |
+------+
|    1 |
|    5 |
|   13 |
|   27 |
+------+
4 rows in set (0.00 sec)

mysql> select num1 from tb11 where exists (select num2 from tb12 where num2 = 100);
Empty set (0.00 sec)

4、带 in 关键字的子查询

mysql> select num1 from tb11 where num1 in (select num2 from tb12);
+------+
| num1 |
+------+
|    3 |
+------+
1 row in set (0.00 sec)

六、合并查询结果

select   字段1,....  from   table1
union  [all]
select  字段1,...  from  table2;
  • union:执行的时候删除重复的记录,自动排序。
  • union all :作用是不删除重复行也不对结果进行自动排序。
#案例:合并a1 和 a2
mysql> select * from a1;
+----+----------+
| id | name     |
+----+----------+
|  1 | liming   |
|  2 | xiaohong |
+----+----------+
2 rows in set (0.00 sec)

mysql> select * from a2;
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

mysql> select * from a1
    -> union select * from a2;
+----+-------------+
| id | name        |
+----+-------------+
|  1 | liming      |
|  2 | xiaohong    |
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
5 rows in set (0.00 sec)

七、为表和字段取别名

1、为表取别名

表名  [as]  表别名
#案例
mysql> select * from a2 as o
    -> where o.id = 2;
+----+-------------+
| id | location    |
+----+-------------+
|  2 | xiaohongjia |
+----+-------------+
1 row in set (0.00 sec)

2、为字段取别名

列名  [as]  列别名
mysql> select id,location as loc from a2 ;
+----+-------------+
| id | loc         |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

八、使用正则表达式查询

选项说明例子
^匹配文本的开始字符‘^b’ 匹配以字母b开头的串
$匹配文本的结束字符‘st$’ 匹配以st结尾的字符串
.匹配任何单个字符‘b.t’匹配任何b和t之间有一个字符的字符串
*匹配零个或多个在它前面的字符‘f*n’匹配字符n前面有任意个字符f的字符串
+匹配前面的字符1次或多次‘ba+’匹配以b开头后面紧跟至少有一个a的字符串
<字符串>匹配包含指定的字符串的文本‘fa’ 匹配包含fa的字符串
[字符串集合]匹配字符集合中的任何一个‘[xz]’ 匹配包含x或者z的字符串
[^]匹配不在括号中的任何字符‘[^abc]’匹配不包含a、b或c的字符串
字符串 {n,}匹配前面的字符串至少n次b{2}匹配2个或更多的b
字符串{n,m}匹配前面的字符串至少n次,至多m次,如果n为0,此参数为可选参数b{2,4}匹配含最少2个,最多4个b的字符串

1、查询以特定字符或字符串开头的记录

mysql> select * from a2  where location regexp '^x';
+----+-------------+
| id | location    |
+----+-------------+
|  2 | xiaohongjia |
+----+-------------+
1 row in set (0.01 sec)

2、查询以特定字符或字符串结尾的记录

mysql> select * from a2  where location regexp 'jia$';
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

3、用符号“.”来替代字符串中的任意一个字符

mysql> select * from a2  where location regexp 'h.n';
+----+-------------+
| id | location    |
+----+-------------+
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
2 rows in set (0.00 sec)

4、使用“*”和“+”来匹配多个字符

mysql> select * from a2  where location regexp 'h+a';
+----+------------+
| id | location   |
+----+------------+
|  3 | wanghanjia |
+----+------------+
1 row in set (0.00 sec)

mysql> select * from a2  where location regexp 'h*a';
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)


5、匹配指定字符串

mysql> select * from a2  where location regexp 'ha';
+----+------------+
| id | location   |
+----+------------+
|  3 | wanghanjia |
+----+------------+
1 row in set (0.00 sec)

6、匹配指定字符中的任意一个

mysql> select * from a2  where location regexp '[xi,hang,jiao]';
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

7、匹配指定字符以外的字符

mysql> select * from a2  where location regexp '[h]';
+----+-------------+
| id | location    |
+----+-------------+
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
2 rows in set (0.00 sec)

mysql> select * from a2  where location regexp '[^h]';
+----+-------------+
| id | location    |
+----+-------------+
|  1 | limingjia   |
|  2 | xiaohongjia |
|  3 | wanghanjia  |
+----+-------------+
3 rows in set (0.00 sec)

8、使用{n,}或{n,m}来指定字符串连续出现的次数

mysql> select * from a2  where location regexp 'x{1,2}';
+----+-------------+
| id | location    |
+----+-------------+
|  2 | xiaohongjia |
+----+-------------+
1 row in set (0.00 sec)

九、MySQL8.0新特性

1、group by 不再隐式排序

2、通用表表达式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值