05-MySQL查询语句

05-MySQL查询语句


  • 05-MySQL查询语句
    • 基本查询语句
    • 单表条件查询
      • in
      • not in
      • between and
      • not between and
      • like
      • 带and的多条件查询
      • 带or的多条件查询
      • 查询结果不重复
      • 排序
      • 分组查询
      • 使用HAVING过滤分组
      • 在group by中使用with rollup关键字,统计记录数量
      • 使用limit限制查询结果的数量
    • 使用聚合函数查询
    • 连接查询
      • 内连接
      • 外链接
    • 子查询
      • ALL
      • exsit
    • 合并查询结果
    • 使用正则表达式查询


基本查询语句

select [字段1,字段2,...,字段n]
from [表或视图]
where [查询条件]
group by [字段1,字段2...,字段n]
order by [字段]  -- 升序(ASC)、降序(DESC)
Limit [从第几条数据查询,查询多少条数据]

单表条件查询

操作符 说明
= 相等
<>,!= 不相等
< 小于
<= 小于或者等于
> 大于
=
大于或者等于
BETWEEN 位于两指之间

测试表结构以及数据初始化

-- 表结构
CREATE TABLE fruits
(
    f_id CHAR(10) NOT NULL,
    s_id INT NOT NULL,
    f_name CHAR(255) NOT NULL,
    f_price DECIMAL(8,2) NOT NULL,
    PRIMARY KEY (f_id)
);
-- 测试数据
INSERT INTO fruits ( f_id, s_id, f_name, f_price )
VALUES
    ( 'a1', 101, 'apple', 5.2 ),
    ( 'b1', 101, 'blackberry', 10.2 ),
    ( 'bs1', 102, 'orange', 11.2 ),
    ( 'bs2', 105, 'melon', 8.2 ),
    ( 't1', 102, 'banana', 10.3 ),
    ( 't2', 102, 'grape', 5.3 ),
    ( 'o2', 103, 'coconut', 9.3 ),
    ( 'c0', 101, 'cherry', 9.2 ),
    ( 'a2', 103, 'apricot', 2.2 ),
    ( 'l2', 104, 'lemon', 6.4 ),
    ( 'b2', 104, 'berry', 7.6 ),
    ( 'm1', 106, 'mango', 15.7 ),
    ( 'm2', 105, 'xbabay', 2.6 ),
    ( 't4', 107, 'xbababa', 3.6 ),
    ( 'm3', 105, 'xxtt', 11.6 ),
    ( 'b5', 107, 'xxxx', 3.6 );

查询全部水果

mysql> select * from fruits;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    9.20 |
| l2   |  104 | lemon      |    6.40 |
| m1   |  106 | mango      |   15.70 |
| m2   |  105 | xbabay     |    2.60 |
| m3   |  105 | xxtt       |   11.60 |
| o2   |  103 | coconut    |    9.30 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)

查询价格小于10的水果的名称

mysql> select f_name,f_price from fruits where f_price <10;
+---------+---------+
| f_name  | f_price |
+---------+---------+
| apple   |    5.20 |
| apricot |    2.20 |
| berry   |    7.60 |
| xxxx    |    3.60 |
| melon   |    8.20 |
| cherry  |    9.20 |
| lemon   |    6.40 |
| xbabay  |    2.60 |
| coconut |    9.30 |
| grape   |    5.30 |
| xbababa |    3.60 |
+---------+---------+
11 rows in set (0.00 sec)

in

使用in关键字,查询s_id为101和102的记录

mysql> select s_id,f_name,f_price from fruits where s_id in (101,102) order by f_name;
+------+------------+---------+
| s_id | f_name     | f_price |
+------+------------+---------+
|  101 | apple      |    5.20 |
|  102 | banana     |   10.30 |
|  101 | blackberry |   10.20 |
|  101 | cherry     |    9.20 |
|  102 | grape      |    5.30 |
|  102 | orange     |   11.20 |
+------+------------+---------+
6 rows in set (0.00 sec)

not in

使用not in关键字,查询s_id不等于101也不等于102的记录

mysql> select s_id,f_name,f_price from fruits where s_id
    -> not in (101,102)
    -> order by f_name;
+------+---------+---------+
| s_id | f_name  | f_price |
+------+---------+---------+
|  103 | apricot |    2.20 |
|  104 | berry   |    7.60 |
|  103 | coconut |    9.30 |
|  104 | lemon   |    6.40 |
|  106 | mango   |   15.70 |
|  105 | melon   |    8.20 |
|  107 | xbababa |    3.60 |
|  105 | xbabay  |    2.60 |
|  105 | xxtt    |   11.60 |
|  107 | xxxx    |    3.60 |
+------+---------+---------+
10 rows in set (0.00 sec)

between and

使用between and 的范围查询价格在2元以及10.2元之间的水果名称以及价格

mysql> select f_name,f_price
    -> from fruits
    -> where f_price between 2.00 and 10.20;
+------------+---------+
| f_name     | f_price |
+------------+---------+
| apple      |    5.20 |
| apricot    |    2.20 |
| blackberry |   10.20 |
| berry      |    7.60 |
| xxxx       |    3.60 |
| melon      |    8.20 |
| cherry     |    9.20 |
| lemon      |    6.40 |
| xbabay     |    2.60 |
| coconut    |    9.30 |
| grape      |    5.30 |
| xbababa    |    3.60 |
+------------+---------+
12 rows in set (0.00 sec)

not between and

查询价格不在2元和10.2元之间的水果名称和价格

mysql> select f_name,f_price
    -> from fruits
    -> where f_price not between 2.00 and 10.20;
+--------+---------+
| f_name | f_price |
+--------+---------+
| orange |   11.20 |
| mango  |   15.70 |
| xxtt   |   11.60 |
| banana |   10.30 |
+--------+---------+
4 rows in set (0.00 sec)

like

查询以b字母开头的水果

mysql> select f_id,f_name
    -> from fruits
    -> where f_name like 'b%';
+------+------------+
| f_id | f_name     |
+------+------------+
| b1   | blackberry |
| b2   | berry      |
| t1   | banana     |
+------+------------+
3 rows in set (0.00 sec)

查询包含b的水果

mysql> select f_id,f_name
    -> from fruits
    -> where f_name like '%b%';
+------+------------+
| f_id | f_name     |
+------+------------+
| b1   | blackberry |
| b2   | berry      |
| m2   | xbabay     |
| t1   | banana     |
| t4   | xbababa    |
+------+------------+
5 rows in set (0.00 sec)

通配符_,一次只能匹配任意一个字符

mysql> select f_id ,f_name
    -> from fruits
    -> where f_name like '____y';
+------+--------+
| f_id | f_name |
+------+--------+
| b2   | berry  |
+------+--------+
1 row in set (0.00 sec)

带and的多条件查询

查询s_id为101,f_price >=5的数据

mysql> select f_id,f_price,f_name
    -> from fruits where s_id ='101' and f_price >=5;
+------+---------+------------+
| f_id | f_price | f_name     |
+------+---------+------------+
| a1   |    5.20 | apple      |
| b1   |   10.20 | blackberry |
| c0   |    9.20 | cherry     |
+------+---------+------------+
3 rows in set (0.00 sec)

带or的多条件查询

mysql> select f_id,f_name,f_price from fruits
    -> where s_id= 101 or s_id =102;
+------+------------+---------+
| f_id | f_name     | f_price |
+------+------------+---------+
| a1   | apple      |    5.20 |
| b1   | blackberry |   10.20 |
| bs1  | orange     |   11.20 |
| c0   | cherry     |    9.20 |
| t1   | banana     |   10.30 |
| t2   | grape      |    5.30 |
+------+------------+---------+
6 rows in set (0.00 sec)

查询结果不重复

mysql> select distinct s_id from fruits;
+------+
| s_id |
+------+
|  101 |
|  103 |
|  104 |
|  107 |
|  102 |
|  105 |
|  106 |
+------+
7 rows in set (0.00 sec)

排序

按照f_name升序,按照价格降序

mysql> select f_name ,f_price
    -> from fruits
    -> order by f_name,f_price DESC;
+------------+---------+
| f_name     | f_price |
+------------+---------+
| apple      |    5.20 |
| apricot    |    2.20 |
| banana     |   10.30 |
| berry      |    7.60 |
| blackberry |   10.20 |
| cherry     |    9.20 |
| coconut    |    9.30 |
| grape      |    5.30 |
| lemon      |    6.40 |
| mango      |   15.70 |
| melon      |    8.20 |
| orange     |   11.20 |
| xbababa    |    3.60 |
| xbabay     |    2.60 |
| xxtt       |   11.60 |
| xxxx       |    3.60 |
+------------+---------+
16 rows in set (0.00 sec)

分组查询

根据s_id 对fruits表进行分组

mysql> select s_id,group_concat(f_name) as Names
    -> from fruits
    -> group by s_id;
+------+-------------------------+
| s_id | Names                   |
+------+-------------------------+
|  101 | apple,blackberry,cherry |
|  102 | orange,banana,grape     |
|  103 | apricot,coconut         |
|  104 | berry,lemon             |
|  105 | melon,xbabay,xxtt       |
|  106 | mango                   |
|  107 | xxxx,xbababa            |
+------+-------------------------+
7 rows in set (0.00 sec)

使用HAVING过滤分组

根据s_id对fruits表中的额数据进行分组,并显示水果种类大于1的分组信息:

mysql> select s_id ,group_concat(f_name) as Names
    -> from fruits
    -> group by s_id having count(f_name) >1;
+------+-------------------------+
| s_id | Names                   |
+------+-------------------------+
|  101 | apple,blackberry,cherry |
|  102 | orange,banana,grape     |
|  103 | apricot,coconut         |
|  104 | berry,lemon             |
|  105 | melon,xbabay,xxtt       |
|  107 | xxxx,xbababa            |
+------+-------------------------+
6 rows in set (0.00 sec)

在group by中使用with rollup关键字,统计记录数量

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

mysql> select s_id,count(*) as total
    -> from fruits
    -> group by s_id with rollup;
+------+-------+
| s_id | total |
+------+-------+
|  101 |     3 |
|  102 |     3 |
|  103 |     2 |
|  104 |     2 |
|  105 |     3 |
|  106 |     1 |
|  107 |     2 |
| NULL |    16 |
+------+-------+
8 rows in set (0.00 sec)

在最后添加了一行,改行的total值正好是上面所有值之和

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

语法:

limit [位置偏移量,行数]  
-- 从第二条记录开始,返回四条数据
mysql> select * from fruits limit 2,4;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
+------+------+------------+---------+
4 rows in set (0.00 sec)

使用聚合函数查询

函数 作用
AVG() 返回某列的平均值
COUNT() 返回某列的行数
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列值的和

连接查询

初始化表结构以及数据


create table suppliers
(
    s_id int not null auto_increment,
    s_name char(50) not null,
    s_city char(50) null,
    s_zip char(50) null,
    s_call char(50) not null,
    primary key (s_id)
);

insert into suppliers
(s_id,s_name,s_city,s_zip,s_call) values
(101,'FastFruit Inc.','Tianjin  300000','48075'),
(102,'LT Supplies','Chongqing','400000','44333'),
(103,'ACME','Shanghai','200000','90046'),
(104,'FNK Inc.','Zhongshan','528437','11111'),
(105,'Good Set','Taiyuang','030000','22222'),
(106,'Just Eat Ours','Beijing','010','45678'),
(107,'DK Inc.','Zhangzhou','450000','33332');
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| f_id    | char(10)     | NO   | PRI | NULL    |       |
| s_id    | int(11)      | NO   |     | NULL    |       |
| f_name  | char(255)    | NO   |     | NULL    |       |
| f_price | decimal(8,2) | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
+--------+----------+------+-----+---------+----------------+
| Field  | Type     | Null | Key | Default | Extra          |
+--------+----------+------+-----+---------+----------------+
| s_id   | int(11)  | NO   | PRI | NULL    | auto_increment |
| s_name | char(50) | NO   |     | NULL    |                |
| s_city | char(50) | YES  |     | NULL    |                |
| s_zip  | char(50) | YES  |     | NULL    |                |
| s_call | char(50) | NO   |     | NULL    |                |
+--------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

两个表通过s_id进行链接。

内连接

只有满足条件的数据才会返回

mysql> select suppliers.s_id,s_name,f_name,f_price
    -> from fruits,suppliers
    -> where fruits.s_id = suppliers.s_id;
+------+----------------+------------+---------+
| s_id | s_name         | f_name     | f_price |
+------+----------------+------------+---------+
|  101 | FastFruit Inc. | apple      |    5.20 |
|  103 | ACME           | apricot    |    2.20 |
|  101 | FastFruit Inc. | blackberry |   10.20 |
|  104 | FNK Inc.       | berry      |    7.60 |
|  107 | DK Inc.        | xxxx       |    3.60 |
|  102 | LT Supplies    | orange     |   11.20 |
|  105 | Good Set       | melon      |    8.20 |
|  101 | FastFruit Inc. | cherry     |    9.20 |
|  104 | FNK Inc.       | lemon      |    6.40 |
|  106 | Just Eat Ours  | mango      |   15.70 |
|  105 | Good Set       | xbabay     |    2.60 |
|  105 | Good Set       | xxtt       |   11.60 |
|  103 | ACME           | coconut    |    9.30 |
|  102 | LT Supplies    | banana     |   10.30 |
|  102 | LT Supplies    | grape      |    5.30 |
|  107 | DK Inc.        | xbababa    |    3.60 |
+------+----------------+------------+---------+
16 rows in set (0.00 sec)
mysql> select suppliers.s_id,s_name,f_name,f_price
    -> from fruits inner join suppliers
    -> on fruits.s_id = suppliers.s_id;
+------+----------------+------------+---------+
| s_id | s_name         | f_name     | f_price |
+------+----------------+------------+---------+
|  101 | FastFruit Inc. | apple      |    5.20 |
|  103 | ACME           | apricot    |    2.20 |
|  101 | FastFruit Inc. | blackberry |   10.20 |
|  104 | FNK Inc.       | berry      |    7.60 |
|  107 | DK Inc.        | xxxx       |    3.60 |
|  102 | LT Supplies    | orange     |   11.20 |
|  105 | Good Set       | melon      |    8.20 |
|  101 | FastFruit Inc. | cherry     |    9.20 |
|  104 | FNK Inc.       | lemon      |    6.40 |
|  106 | Just Eat Ours  | mango      |   15.70 |
|  105 | Good Set       | xbabay     |    2.60 |
|  105 | Good Set       | xxtt       |   11.60 |
|  103 | ACME           | coconut    |    9.30 |
|  102 | LT Supplies    | banana     |   10.30 |
|  102 | LT Supplies    | grape      |    5.30 |
|  107 | DK Inc.        | xbababa    |    3.60 |
+------+----------------+------------+---------+
16 rows in set (0.00 sec)

外链接

left join 返回包括左表中的额所有记录和右表中链接字段相等的记录

right join 返回包括右表中的所有记录和左表中的链接字段相等的记录

mysql> select f.f_id,f.f_name,f.f_price,s.s_id,s.s_name
    -> from fruits f left join suppliers s on f.s_id = s.s_id;
+------+------------+---------+------+----------------+
| f_id | f_name     | f_price | s_id | s_name         |
+------+------------+---------+------+----------------+
| a1   | apple      |    5.20 |  101 | FastFruit Inc. |
| a2   | apricot    |    2.20 |  103 | ACME           |
| b1   | blackberry |   10.20 |  101 | FastFruit Inc. |
| b2   | berry      |    7.60 |  104 | FNK Inc.       |
| b5   | xxxx       |    3.60 |  107 | DK Inc.        |
| bs1  | orange     |   11.20 |  102 | LT Supplies    |
| bs2  | melon      |    8.20 |  105 | Good Set       |
| c0   | cherry     |    9.20 |  101 | FastFruit Inc. |
| l2   | lemon      |    6.40 |  104 | FNK Inc.       |
| m1   | mango      |   15.70 |  106 | Just Eat Ours  |
| m2   | xbabay     |    2.60 |  105 | Good Set       |
| m3   | xxtt       |   11.60 |  105 | Good Set       |
| o2   | coconut    |    9.30 |  103 | ACME           |
| t1   | banana     |   10.30 |  102 | LT Supplies    |
| t2   | grape      |    5.30 |  102 | LT Supplies    |
| t4   | xbababa    |    3.60 |  107 | DK Inc.        |
+------+------------+---------+------+----------------+
16 rows in set (0.00 sec)
mysql> select f.f_id,f.f_name,f.f_price,s.s_id,s.s_name
    -> from fruits f inner join suppliers s on f.s_id = s.s_id
    -> and s.s_id = 101;
+------+------------+---------+------+----------------+
| f_id | f_name     | f_price | s_id | s_name         |
+------+------------+---------+------+----------------+
| a1   | apple      |    5.20 |  101 | FastFruit Inc. |
| b1   | blackberry |   10.20 |  101 | FastFruit Inc. |
| c0   | cherry     |    9.20 |  101 | FastFruit Inc. |
+------+------------+---------+------+----------------+
3 rows in set (0.00 sec)

子查询

初始化数据

mysql> create table tb1(num1 int not null);
Query OK, 0 rows affected (0.02 sec)

mysql> create table tb1(num2 int not null);
ERROR 1050 (42S01): Table 'tb1' already exists
mysql> create table tb2(num2 int not null);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tb1 value (1),(5),(13),(27);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into tb2 value (6),(14),(11),(20);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

查询出tb1 中大于tb2中任意值的集合

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

mysql> select num1 from tb1 where num1> any (select num2 from tb2);
+------+
| num1 |
+------+
|   13 |
|   27 |
+------+
2 rows in set (0.00 sec)

ALL

查询tb1中大于tb2所有值的值

mysql> select num1 from tb1 where num1 > all(select num2 from tb2);
+------+
| num1 |
+------+
|   27 |
+------+
1 row in set (0.00 sec)

exsit

查询suppliers表中是否存在s_id=107的供应商,如果存在,则返回fruits表

mysql> select * from fruits where exists 
(select s_name from suppliers where s_id =107);
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    9.20 |
| l2   |  104 | lemon      |    6.40 |
| m1   |  106 | mango      |   15.70 |
| m2   |  105 | xbabay     |    2.60 |
| m3   |  105 | xxtt       |   11.60 |
| o2   |  103 | coconut    |    9.30 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)

和查询条件一起用:

mysql> select * from fruits
    -> where f_price >10.20 and exists
    -> (select s_name from suppliers where s_id =107);
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1  |  102 | orange |   11.20 |
| m1   |  106 | mango  |   15.70 |
| m3   |  105 | xxtt   |   11.60 |
| t1   |  102 | banana |   10.30 |
+------+------+--------+---------+
4 rows in set (0.00 sec)

合并查询结果

查询出价格大于9块的水果,查询出s_id 101 和103的水果,然后合并。

mysql> select s_id ,f_name,f_price
    -> from fruits where f_price <9.0
    -> union all
    -> select s_id ,f_name,f_price
    -> from fruits
    -> where s_id in (101,103);
+------+------------+---------+
| s_id | f_name     | f_price |
+------+------------+---------+
|  101 | apple      |    5.20 |
|  103 | apricot    |    2.20 |
|  104 | berry      |    7.60 |
|  107 | xxxx       |    3.60 |
|  105 | melon      |    8.20 |
|  104 | lemon      |    6.40 |
|  105 | xbabay     |    2.60 |
|  102 | grape      |    5.30 |
|  107 | xbababa    |    3.60 |
|  101 | apple      |    5.20 |
|  103 | apricot    |    2.20 |
|  101 | blackberry |   10.20 |
|  101 | cherry     |    9.20 |
|  103 | coconut    |    9.30 |
+------+------------+---------+
14 rows in set (0.00 sec)

使用正则表达式查询

选项 说明 例子 匹配值示例
^ 匹配文本的开始字符 ‘^b’匹配以字母b开头的字符串 book,big
||st | 匹 配 文 本 结 束 字 符 | ′ s t ’匹配以st结尾的字符串 test,resist
. 匹配任何单个字符 ‘b.t’匹配任何b和t之间有一个字符 bit,bat,but
* 匹配0个或者多个在它前面的字符 ‘f*n’匹配字符n前面有任意个字符f fn,fan
+ 匹配前面的字符1次或多次 ‘ba+’匹配以b开头后面紧跟至少一个a ba,bay,bare
<字符串> 匹配包含指定的字符串的文本 ‘fa’ fan,afa,faad
[字符集合] 匹配字符集合中的任何一个字符 ‘[xz]’匹配x或者z dizzy,zebra,x=r-ray
[^] 匹配不在括号中的任意字符 ‘[^abc]’匹配任何不包括a,b,c的字符串 desk,fox,f8ke
字符串{n,} 匹配前面的字符串至少n次 b{2}匹配2个或者更多的b bbb,bbbb
字符串{n,m} 匹配前面的字符串至少n次,至多m次,如果n为0,此参数为可选参数 b{2,4}匹配至少2个,最多4个b bb,bbb,bbbb

你可能感兴趣的:(MySQL)