mysql>SELECT (数学+语文+英语+物理+化学) AS 总分 FROM demo_2; +--------+ | 总分 | +--------+ |397| |388| |436| |352| +--------+ 4rowsinset (0.00 sec)
mysql>SELECT 姓名 (数学+语文+英语+物理+化学) AS 总分 FROM demo_2; ERROR 1305 (42000): FUNCTION stuinfo.姓名 does not exist mysql>SELECT 姓名 ,(数学+语文+英语+物理+化学) AS 总分 FROM demo_2; +--------+--------+ | 姓名 | 总分 | +--------+--------+ | Jack |397| | Mike |388| | Han |436| | Xuan |352| +--------+--------+
给表起别名:
1 2 3 4 5 6 7 8 9 10 11
mysql>SELECT stu.name ,stu.addr FROM stu_info_table AS stu; +-------+--------+ | name | addr | +-------+--------+ | Mike | 南京 | | XUAN | 南京 | | Han | 南京 | | Jack | 成都 | | Cling | 深圳 | +-------+--------+ 5rowsinset (0.01 sec)
mysql>SELECT*FROM stu_info_table LIMIT 3; +------+--------+ | name | addr | +------+--------+ | Mike | 南京 | | XUAN | 南京 | | Han | 南京 | +------+--------+ 3rowsinset (0.00 sec)
每页显示 N 条,则公式为:LIMIT (第几页-1)*N , N
对查询结果排序
1
ORDERBY<字段名> [ASC|DESC]
其中, ASC 代表升序,DESC 代表降序;默认升序 ;
单字段进行排序:
1 2 3 4 5 6 7 8 9
mysql>SELECT*FROM demo_2 ORDERBY math DESC; +--------+--------+--------+--------+--------+--------+ | 姓名 | 数学 | 语文 | 英语 | 物理 | 化学 | +--------+--------+--------+--------+--------+--------+ | Jack |98|87|87|68|57| | Han |87|98|97|96|58| | Mike |76|87|68|59|98| | Xuan |67|57|86|58|84| +--------+--------+--------+--------+--------+--------+
多字段进行排序:
1 2 3 4 5 6 7 8 9
mysql>SELECT Chinese, Math FROM demo_2 ORDERBY Chinese ,Math DESC; +---------+------+ | Chinese | Math | +---------+------+ |57|67| |87|98| |87|76| |98|87| +---------+------+
mysql>SELECT Chinese, Math FROM demo_2 ORDERBY Chinese DESC,Math DESC; +---------+------+ | Chinese | Math | +---------+------+ |98|87| |87|98| |87|76| |57|67| +---------+------+ 4rowsinset (0.00 sec)
DESC 关键字只对前面的列进行降序排列,在这里只对 height 字段进行降序。因此,height 按降序排序,而 name 仍按升序排序。如果想在多个列上进行降序排序,必须对每个列指定 DESC 关键字 。
####IN mysql>SELECT*FROM demo_2 ->WHERE Math IN(98,76); +------+------+---------+---------+---------+-----------+ | name | Math | Chinese | English | Physics | Chemistry | +------+------+---------+---------+---------+-----------+ | Jack |98|87|87|68|57| | Mike |76|87|68|59|98| +------+------+---------+---------+---------+-----------+
####BETWEENAND mysql>SELECT Chinese FROM demo_2 ->WHERE Chinese BETWEEN87AND98; +---------+ | Chinese | +---------+ |87| |87| |98| +---------+ ####XOR mysql>SELECT*FROM demo_2 ->WHERE MATH >70 XOR Physics <70; +------+------+---------+---------+---------+-----------+ | name | Math | Chinese | English | Physics | Chemistry | +------+------+---------+---------+---------+-----------+ | Han |87|98|97|96|58| | Xuan |67|57|86|58|84| +------+------+---------+---------+---------+-----------+
LIKE 模糊查询:% 代表任何长度的字符串,字符串的长度可以为 0;_ 只能代表单个字符,字符的长度不能为 0;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
mysql>SELECT*FROM demo_1 ->WHERE `name` LIKE "%an%"; +-------+--------+------+--------+ | id | name | age | addr | +-------+--------+------+--------+ |12336| QuanHa |20| 四川 | |12321| Han |20| 天津 | +-------+--------+------+--------+ 2rowsinset (0.00 sec)
mysql>SELECT*FROM demo_1 ->WHERE `name` LIKE "_a%"; +-------+------+------+--------+ | id | name | age | addr | +-------+------+------+--------+ |12321| Han |20| 天津 | +-------+------+------+--------+
mysql>SELECT*FROM demo_1 ->WHERE `name` LIKEBINARY "han"; Emptyset (0.00 sec) mysql>SELECT*FROM demo_1 ->WHERE `name` LIKEBINARY "Han"; +-------+------+------+--------+ | id | name | age | addr | +-------+------+------+--------+ |12321| Han |20| 天津 | +-------+------+------+--------+ 1rowinset (0.00 sec)
注意:在确定使用通配符后,除非绝对有必要,否则不要把它们用在字符串的开始处。把通配符置于搜索模式的开始处,搜索起来是最慢的。 WHERE 查询条件中不可以使用字段别名 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
mysql>SELECT name AS 姓名,Math AS 数学 FROM demo_2 ->WHERE 数学>60; ERROR 1054 (42S22): Unknowncolumn'数学'in'where clause' mysql>SELECT name AS 姓名,Math AS 数学 FROM demo_2 ->HAVING 数学>60; +--------+--------+ | 姓名 | 数学 | +--------+--------+ | Jack |98| | Mike |76| | Han |87| | Xuan |67| +--------+--------+ 4rowsinset (0.00 sec)
分组查询
1
GROUPBY<字段名>
在数据统计时,GROUP BY 关键字经常和聚合函数一起使用。聚合函数包括 COUNT(),SUM(),AVG(),MAX() 和 MIN()等:
mysql>SELECT*FROM demo_2; +------+------+------+---------+---------+---------+-----------+ | SEX | name | Math | Chinese | English | Physics | Chemistry | +------+------+------+---------+---------+---------+-----------+ | 男 | Jack |98|87|87|68|57| | 男 | Mike |76|87|68|59|98| | 女 | Han |87|98|97|96|58| | 女 | Xuan |67|57|86|58|84| +------+------+------+---------+---------+---------+-----------+ 4rowsinset (0.00 sec)
mysql>SELECT SEX ,AVG(Math) AS 数学平均分 FROM demo_2 ->GROUPBY SEX; +------+-----------------+ | SEX | 数学平均分 | +------+-----------------+ | 女 |77.0000| | 男 |87.0000| +------+-----------------+ 2rowsinset (0.00 sec)
GROUP BY 关键字可以和 GROUP_CONCAT() 函数一起使用。GROUP_CONCAT() 函数会把每个分组的字段值都显示出来:
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql>SELECT `name`, SEX FROM demo_2 ->GROUPBY SEX; ERROR 1055 (42000): Expression 1ofSELECT list isnotinGROUPBY clause andcontains nonaggregated column'stuinfo.demo_2.name' which isnot functionally dependent on columns inGROUPBY clause; this is incompatible with sql_mode=only_full_group_by ### 直接根据SEX分组打印信息会报错 mysql>SELECT SEX, GROUP_CONCAT(name) AS name FROM demo_2 ->GROUPBY SEX; +------+-----------+ | SEX | name | +------+-----------+ | 女 | Han,Xuan | | 男 | Jack,Mike | +------+-----------+ 2rowsinset (0.00 sec)
WITH POLLUP 关键字用来在所有记录的最后加上一条记录,这条记录是上面所有记录的总和,即统计记录数量。
1 2 3 4 5 6 7 8 9 10
mysql>SELECT SEX, GROUP_CONCAT(name) AS name FROM demo_2 ->GROUPBY SEX WITHROLLUP; +------+--------------------+ | SEX | name | +------+--------------------+ | 女 | Han,Xuan | | 男 | Jack,Mike | |NULL| Han,Xuan,Jack,Mike | +------+--------------------+ 3rowsinset (0.01 sec)
过滤分组
1
HAVING<查询条件>
HAVING 关键字和 WHERE 关键字都可以用来过滤数据,且 HAVING 支持 WHERE 关键字中所有的操作符和语法。但是 WHERE 和 HAVING 关键字也存在以下几点差异:
一般情况下,WHERE 用于过滤数据行,而 HAVING 用于过滤分组。
WHERE 查询条件中不可以使用聚合函数,而 HAVING 查询条件中可以使用聚合函数。
WHERE 在数据分组前进行过滤,而 HAVING 在数据分组后进行过滤 ,所以 HAVING 绝大多数时候应该搭配 GROUP BY 使用!
WHERE 针对数据库文件进行过滤,而 HAVING 针对查询结果进行过滤。也就是说,WHERE 根据数据表中的字段直接进行过滤,而 HAVING 是根据前面已经查询出的字段进行过滤。
#对于第4点 mysql>SELECT name FROM demo_2 ->HAVING Math>60; ERROR 1054 (42S22): Unknowncolumn'Math'in'having clause' mysql>SELECT name FROM demo_2 ->WHERE Math>60; +------+ | name | +------+ | Jack | | Mike | | Han | | Xuan | +------+ #对于第5点 mysql>SELECT Math AS 数学 FROM demo_2 ->WHERE 数学>60; ERROR 1054 (42S22): Unknowncolumn'数学'in'where clause' mysql>SELECT Math AS 数学 FROM demo_2 ->HAVING 数学>60; +--------+ | 数学 | +--------+ |98| |76| |87| |67| +--------+ 4rowsinset (0.00 sec)
mysql>SELECT name FROM customer_info ->WHERE room_id = ( ->SELECT room_id FROM customer_info WHERE `name`="李华") ->AND `name`!='李华'; +--------+ | name | +--------+ | 李斯 | +--------+
2)查询李华住的房间的价格(多次嵌套,横跨三张表):
1 2 3 4 5 6 7 8 9 10 11
mysql>SELECT price FROM type_info ->WHERE type=( ->SELECT type FROM room_info ->WHERE room_id=( ->SELECT room_id FROM customer_info ->WHERE name = "李华")); +-------+ | price | +-------+ |150| +-------+
多行子查询:
1)查询状态为空的所有房间的价格:
1 2 3 4 5 6 7 8 9 10 11 12
mysql>SELECT price FROM type_info ->WHERE type IN( ->SELECTDISTINCT type FROM room_info ->WHERE state=0); +-------+ | price | +-------+ |150| |210| |200| |400| +-------+
mysql>SELECT*FROM emp; +------+---------+------+------------+ | id | name | mgr | department | +------+---------+------+------------+ |1| Han |NULL|10| |2| Jack |1|12| |3| Tom |1|12| |4| Cling |2|20| |5| Black |3|20| |6| JayChou |1|12| +------+---------+------+------------+
需求:找到和 Jack 所在部门相同且上级也相同的人:
1 2 3 4 5 6 7 8 9 10 11 12
mysql>SELECT*FROM emp ->WHERE (mgr,department)=( ->SELECT mgr,department FROM emp ->WHERE name="Jack") ->&& name!="Jack"; +------+---------+------+------------+ | id | name | mgr | department | +------+---------+------+------------+ |3| Tom |1|12| |6| JayChou |1|12| +------+---------+------+------------+ 2rowsinset (0.00 sec)
子查询语句返回多列结果,分别与等号前的 mgr 和 department 匹配。
合并查询
通常使用 UNION ALL 或 UNION 关键字合并多张表的查询结果。前者不会去重,后者会去重。比如有这样一个需求:NO.1 中学和 NO.2 中学联考,并混合排名:
mysql>SELECT*FROM NO1SC.stu_info; +------+------+-------+ | name | age | score | +------+------+-------+ | Jack |18|150| | Mike |20|149| | Dan |19|130| | Xuan |19|141| +------+------+-------+ 4rowsinset (0.00 sec)
mysql>SELECT*FROM NO2SC.stu_info; +---------+------+-------+ | name | age | score | +---------+------+-------+ | JayChou |17|120| | Black |28|120| | Han |21|147| +---------+------+-------+ 3rowsinset (0.00 sec)
mysql>SELECT*FROM NO1SC.stu_info ->UNIONALL ->SELECT*FROM NO2SC.stu_info ->ORDERBY score DESC; +---------+------+-------+ | name | age | score | +---------+------+-------+ | Jack |18|150| | Mike |20|149| | Han |21|147| | Xuan |19|141| | Dan |19|130| | Black |28|120| | JayChou |17|120| +---------+------+-------+