目录
🛻 查询数据
🛻基本查询语句
🛻单表查询
🚕查询所有字段
🚕查询指定字段
🚕查询指定记录
🚕带in关键字的查询
🚕带between and的范围查询
🚕带like的字符匹配查询
🚕查询空值
🚕带and的多条件查询
🚕带or的多条件查询
🚕查询结果不重复
🚕对查询结果排序
🛻单列排序
🛻多列排序
🛻指定排序方向
🚕分组查询
🛻聚合函数查询
🚕count()函数
🚕sum()函数
🚕avg()函数
🚕max()函数
查询数据
基本查询语句
SELECT {* | < 字段列表 >} [ FROM < 表 1>, < 表 2>.... [ where < 表达式 > ] [ group by ] [ having ] [ order by<..> ] [ limit <...> ]
mysql> 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)
-> );
添加内容:
mysql> 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.2'),
-> ('c0',101,'cherry','3.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 | 3.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.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
| t4 | 107 | xbababa | 3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)
查询指定字段
select 字段名 from 表名;
mysql> select f_name from fruits;
+------------+
| f_name |
+------------+
| apple |
| apricot |
| blackberry |
| berry |
| xxxx |
| orange |
| melon |
| cherry |
| lemon |
| mango |
| xbabay |
| xxtt |
| coconut |
| banana |
| grape |
| xbababa |
+------------+
16 rows in set (0.00 sec)
2.查询多个字段 使用select声明,可以获取多个字段下的数据,只需要在关键字select后面指定要查询的字段的名称,不同字段名称之间用逗号分隔,最后一个字段后面不需要加逗号
mysql> select f_id,s_id,f_name from fruits;
+------+------+------------+
| f_id | s_id | f_name |
+------+------+------------+
| a1 | 101 | apple |
| a2 | 103 | apricot |
| b1 | 101 | blackberry |
| b2 | 104 | berry |
| b5 | 107 | xxxx |
| bs1 | 102 | orange |
| bs2 | 105 | melon |
| c0 | 101 | cherry |
| l2 | 104 | lemon |
| m1 | 106 | mango |
| m2 | 105 | xbabay |
| m3 | 105 | xxtt |
| o2 | 103 | coconut |
| t1 | 102 | banana |
| t2 | 102 | grape |
| t4 | 107 | xbababa |
+------+------+------------+
16 rows in set (0.00 sec)
查询指定记录
select 字段1,字段2....字段n from 表名 where 查询条件;
操作符
|
说明
|
=
|
相等
|
<>,!=
|
不相等
|
<
|
小于
|
<=
|
小于等于
|
>
|
大于等于
|
>=
|
大于等于
|
BETWEEN
|
位于两端之间
|
mysql> select f_name,f_price
-> from fruits
-> where f_price = 10.2;
+------------+---------+
| f_name | f_price |
+------------+---------+
| blackberry | 10.20 |
+------------+---------+
1 row in set (0.01 sec)
带in关键字的查询
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 | 3.20 |
| 102 | grape | 5.30 |
| 102 | orange | 11.20 |
+------+------------+---------+
6 rows in set (0.00 sec)
带between and的范围查询
mysql> select f_name,f_price
-> from fruits
-> where f_price between 2.00 and 3.00;
+---------+---------+
| f_name | f_price |
+---------+---------+
| apricot | 2.20 |
| xbabay | 2.60 |
+---------+---------+
2 rows in set (0.00 sec)
带like的字符匹配查询
通配符是一种在SQL的where条件子句中拥有特殊意思的字符,SQL语句中支持多种通配符,可以和like一起使用的通配符有‘%’和‘_’。
1.百分号(%)通配符,匹配任意长度的字符,甚至包括零字符
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)
2.下划线(__)通配符,一次只能匹配任意一个字符
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)
查询空值
数据表创建的时候,设计者可以指定某列中是否可以包含空值(NULL)。空值不同于0,也不同于空字符串。空 值一般表示数据未知、不适用或将在以后添加数据。在select语句中使用IS NULL子句,可以查询某字段内容为 空的记录。
mysql> create table customers
-> (
-> c_id int not null auto_increment,
-> c_name char(50) not null,
-> c_address char(50) null,
-> c_city char(50) null,
-> c_zip char(50) null,
-> c_contact char(50) null,
-> c_email char(50) null,
-> primary key(c_id)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into customers(c_id,c_name,c_address,c_city,c_zip,c_contact,c_email)
-> values(10001,'RedHook','200
'> Street','Tianjin','300000','LiMing','LMing@163.com'),
-> (10002,'Stars','333 Fromage
'> Lane','Dalian','116000','Zhangbo','Jerry@hotmail.com'),
-> (10003,'Netbhood','1 Sunny Place','Qingdao','266000','LuoCong',NULL),
-> (10004,'JOTO','829 Riverside Drive', 'Haikou','570000','YangShan',
-> 'sam@hotmail.com');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select c_id,c_name,c_email from customers where c_email IS NULL;
+-------+----------+---------+
| c_id | c_name | c_email |
+-------+----------+---------+
| 10003 | Netbhood | NULL |
+-------+----------+---------+
1 row in set (0.00 sec)
带and的多条件查询
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 |
+------+---------+------------+
2 rows in set (0.00 sec)
带or的多条件查询
与and相反,在where声明中使用or操作符,表示只需要满足其中一个条件的记录即可返回。or也可以连接两个甚至多个查询条件,多个条件表达式之间用or分开。
mysql> select s_id,f_name,f_price
-> from fruits
-> where s_id = 101 or s_id = 102;
+------+------------+---------+
| s_id | f_name | f_price |
+------+------------+---------+
| 101 | apple | 5.20 |
| 101 | blackberry | 10.20 |
| 102 | orange | 11.20 |
| 101 | cherry | 3.20 |
| 102 | banana | 10.30 |
| 102 | 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)
对查询结果排序
单列排序
mysql> select f_name from fruits order by f_name;
+------------+
| f_name |
+------------+
| apple |
| apricot |
| banana |
| berry |
| blackberry |
| cherry |
| coconut |
| grape |
| lemon |
| mango |
| melon |
| orange |
| xbababa |
| xbabay |
| xxtt |
| xxxx |
+------------+
16 rows in set (0.00 sec)
多列排序
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| banana | 10.30 |
| berry | 7.60 |
| blackberry | 10.20 |
| cherry | 3.20 |
| coconut | 9.20 |
| 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)
注:在多列进行排序的时候,首先排序的第一列必须有相同的列值,才会对第二列进行排序。如果第一列数据中所有值都是唯一的,将不再对第二列进行排序。
指定排序方向
默认情况下,查询数据按字母升序进行排序(从A~Z),但数据的排序并不仅限于此,还可以使用order by对查询结果进行降序排序(从Z~A),这可以通过关键字DESC实现。
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 | 3.20 |
| coconut | 9.20 |
| 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)
注:与DESC相反ASC是升序
分组查询
mysql> select s_id,count(*) as total from fruits group by s_id;
+------+-------+
| s_id | total |
+------+-------+
| 101 | 3 |
| 102 | 3 |
| 103 | 2 |
| 104 | 2 |
| 105 | 3 |
| 106 | 1 |
| 107 | 2 |
+------+-------+
7 rows in set (0.00 sec)
mysql> select s_id,group_concat(f_name) as name from fruits group by s_id;
+------+-------------------------+
| s_id | name |
+------+-------------------------+
| 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)
mysql> select s_id,group_concat(f_name) as name from fruits group by s_id having
-> count(f_name) > 1;
+------+-------------------------+
| s_id | name |
+------+-------------------------+
| 101 | apple,blackberry,cherry |
| 102 | orange,banana,grape |
| 103 | apricot,coconut |
| 104 | berry,lemon |
| 105 | melon,xbabay,xxtt |
| 107 | xxxx,xbababa |
+------+----------------------
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)
mysql> select * from fruits group by f_id,f_name;
+------+------+------------+---------+
| 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 | 3.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.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
| t4 | 107 | xbababa | 3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)
mysql> create table orderitems
-> (
-> o_num int not null,
-> o_item int not null,
-> f_id char(10) not null,
-> quantity int not null,
-> item_price decimal(8,2) not null,
-> primary key(o_num,o_item)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> insert into orderitems(o_num,o_item,f_id,quantity,item_price)
-> values(30001,1,'a1',10,'5.2'),
-> (30001,2,'b2',3,'7.6'),
-> (30001,3,'bs1',5,'11.2'),
-> (30001,4,'bs2',15,'9.2'),
-> (30002,1,'b3',2,'20.0'),
-> (30003,1,'c0',100,10),
-> (30004,1,'o2',50,'2.50'),
-> (30005,1,'c0',5,'10'),
-> (30005,2,'b1',10,'8.99'),
-> (30005,3,'a2',10,'2.2'),
-> (30005,4,'m1',5,'14.99');
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select o_num,sum(quantity*item_price) as ordertotal
-> from orderitems
-> group by o_num
-> having sum(quantity*item_price) >= 100;
+-------+------------+
| o_num | ordertotal |
+-------+------------+
| 30001 | 268.80 |
| 30003 | 1000.00 |
| 30004 | 125.00 |
| 30005 | 236.85 |
+-------+------------+
4 rows in set (0.00 sec)
mysql> select * from fruits limit 4;
+------+------+------------+---------+
| 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 |
+------+------+------------+---------+
4 rows in set (0.00 sec)
mysql> select * from fruits limit 4,3;
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b5 | 107 | xxxx | 3.60 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
+------+------+--------+---------+
3 rows in set (0.00 sec)
使用聚合函数查询
函数
|
作用
|
AVG()
|
返回某列的平均值
|
COUNT()
|
返回某列的行数
|
MAX()
|
返回某列的最大值
|
MIN()
|
返回某列的最小值
|
SUM()
|
返回某列的和
|
count()函数
Count()函数统计数据表中包含的记录行的总数,或者根据查询结果返回列中包含的数据行数。 Count(*)计算表中总的函数,不管某列有数列或者为空值 Count(字段名)计算指定列下总的行数,计算时将忽略空值的行
mysql> select count(*) as cust_num
-> from customers;
+----------+
| cust_num |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
mysql> select count(c_email) as email_num
-> from customers;
+-----------+
| email_num |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
mysql> select o_num,count(f_id)
-> from orderitems
-> group by o_num;
+-------+-------------+
| o_num | count(f_id) |
+-------+-------------+
| 30001 | 4 |
| 30002 | 1 |
| 30003 | 1 |
| 30004 | 1 |
| 30005 | 4 |
+-------+-------------+
5 rows in set (0.00 sec)
sum()函数
sum()是一个求总和的函数,返回指定列值得总和
mysql> select sum(quantity) as items_total
-> from orderitems
-> where o_num = 30005;
+-------------+
| items_total |
+-------------+
| 30 |
+-------------+
1 row in set (0.00 sec)
mysql> select o_num,sum(quantity) as items_total
-> from orderitems
-> group by o_num;
+-------+-------------+
| o_num | items_total |
+-------+-------------+
| 30001 | 33 |
| 30002 | 2 |
| 30003 | 100 |
| 30004 | 50 |
| 30005 | 30 |
+-------+-------------+
5 rows in set (0.00 sec)
注意:sum()函数在计算时,忽略列值为NULL的行。
avg()函数
mysql> select avg(f_price) as avg_price
-> from fruits
-> where s_id=103;
+-----------+
| avg_price |
+-----------+
| 5.700000 |
+-----------+
1 row in set (0.00 sec)
mysql> select s_id,avg(f_price) as avg_price
-> from fruits
-> group by s_id;
+------+-----------+
| s_id | avg_price |
+------+-----------+
| 101 | 6.200000 |
| 102 | 8.933333 |
| 103 | 5.700000 |
| 104 | 7.000000 |
| 105 | 7.466667 |
| 106 | 15.700000 |
| 107 | 3.600000 |
+------+-----------+
7 rows in set (0.01 sec)
max()函数
max()返回指定列中的最大值
mysql> select max(f_price) as max_price from fruits;
+-----------+
| max_price |
+-----------+
| 15.70 |
+-----------+
1 row in set (0.00 sec)
mysql> select s_id,max(f_price) as max_price from fruits group by s_id ;
+------+-----------+
| s_id | max_price |
+------+-----------+
| 101 | 10.20 |
| 102 | 11.20 |
| 103 | 9.20 |
| 104 | 7.60 |
| 105 | 11.60 |
| 106 | 15.70 |
| 107 | 3.60 |
+------+-----------+
7 rows in set (0.00 sec)