MySQL单表查询实例详解
1、准备数据
以下操作将在该表中进行
create table student ( id int unsigned primary key auto_increment, name char(12) not null, gender enum("male","famale") default "male", age tinyint unsigned not null, hoc_group char(12) not null, html tinyint unsigned not null, css tinyint unsigned not null, js tinyint unsigned not null, sanction enum("大处分","小处分","无") ); insert into student(name,gender,age,hoc_group,html,css,js,sanction) values ("yunya","male",18,"first",88,93,76,"无"), ("jack","male",17,"second",92,81,88,"无"), ("bella","famale",17,"first",72,68,91,"小处分"), ("dairis","famale",18,"third",89,54,43,"大处分"), ("kyle","famale",19,"fifth",31,24,60,"大处分"), ("alice","famale",16,"second",49,23,58,"无"), ("ken","male",16,"third",33,62,17,"大处分"), ("jason","male",21,"fourth",91,92,90,"无"), ("tom","male",20,"fifth",88,72,91,"无"), ("fiona","famale",19,"fourth",60,71,45,"无");
2、查询语法
select distinct(字段名1,字段名2...) from 表名 where 条件 group by 字段名 having 筛选 order by 字段名 asc/desc limit 限制条数;
3、执行顺序
虽然查询的书写语法是上面那样的,但是其内部执行顺序却有些不太一样。
1.通过from找到将要查询的表
2.where规定查询条件,在表记录中逐行进行查询并筛选出符合规则的记录
3.将查到的记录进行字段分组group by,如果没有进行分组,则默认为一组
4.将分组得到的结果进行having筛选,可使用聚和函数(where时不可使用聚合函数)
5.执行select准备打印
6.执行distinct对打印结果进行去重
7.执行ordery by对结果进行排序
8.执行limit对打印结果的条数进行限制
4、select
select主要复负责打印相关的工作
4.1 全部查询
使用select * from 表名
可拿到该表下全部的数据
以下示例将展示使用全部查询拿到student表中所有记录
select * from student; +----+--------+--------+-----+-----------+------+-----+----+-----------+ | id | name | gender | age | hoc_group | html | css | js | sanction | +----+--------+--------+-----+-----------+------+-----+----+-----------+ | 1 | yunya | male | 18 | first | 88 | 93 | 76 | 无 | | 2 | jack | male | 17 | second | 92 | 81 | 88 | 无 | | 3 | bella | famale | 17 | first | 72 | 68 | 91 | 小处分 | | 4 | dairis | famale | 18 | third | 89 | 54 | 43 | 大处分 | | 5 | kyle | famale | 19 | fifth | 31 | 24 | 60 | 大处分 | | 6 | alice | famale | 16 | second | 49 | 23 | 58 | 无 | | 7 | ken | male | 16 | third | 33 | 62 | 17 | 大处分 | | 8 | jason | male | 21 | fourth | 91 | 92 | 90 | 无 | | 9 | tom | male | 20 | fifth | 88 | 72 | 91 | 无 | | 10 | fiona | famale | 19 | fourth | 60 | 71 | 45 | 无 | +----+--------+--------+-----+-----------+------+-----+----+-----------+
4.2 字段查询
使用select 字段名1,字段名2 from 表名可拿到特定字段下相应的数据
以下示例将展示使用字段查询拿到每个学生的html\css\js成绩
select name,html,css,js from student; +--------+------+-----+----+ | name | html | css | js | +--------+------+-----+----+ | yunya | 88 | 93 | 76 | | jack | 92 | 81 | 88 | | bella | 72 | 68 | 91 | | dairis | 89 | 54 | 43 | | kyle | 31 | 24 | 60 | | alice | 49 | 23 | 58 | | ken | 33 | 62 | 17 | | jason | 91 | 92 | 90 | | tom | 88 | 72 | 91 | | fiona | 60 | 71 | 45 | +--------+------+-----+----+
4.3 as 别名
使用select 字段名1 as 别名1, 字段名2 as 别名2 from 表名可将查询到的记录字段修改一个别名
以下示例将展示修改name字段为姓名,修改gender字段为性别,修改age字段为年龄的操作
select name as "姓名", gender as "性别", age as "年龄" from student; +--------+--------+--------+ | 姓名 | 性别 | 年龄 | +--------+--------+--------+ | yunya | male | 18 | | jack | male | 17 | | bella | famale | 17 | | dairis | famale | 18 | | kyle | famale | 19 | | alice | famale | 16 | | ken | male | 16 | | jason | male | 21 | | tom | male | 20 | | fiona | famale | 19 | +--------+--------+--------+
4.4 distinct
使用select distinct(字段名1, 字段名2) from 表名可将查询到的记录做一个取消重复的操作
以下示例将展示使用去重功能来看有多少个小组
select distinct(hoc_group) from student; +-----------+ | hoc_group | +-----------+ | first | | second | | third | | fifth | | fourth | +-----------+
4.5 四则运算
查询结果可进行四则运算,以下示例将展示拿到每个同学三科总分的操作
select name, html+css+js as 总成绩 from student; +--------+-----------+ | name | 总成绩 | +--------+-----------+ | yunya | 257 | | jack | 261 | | bella | 231 | | dairis | 186 | | kyle | 115 | | alice | 130 | | ken | 112 | | jason | 273 | | tom | 251 | | fiona | 176 | +--------+-----------+
4.6 显示格式
使用concat()可将查询结果与任意字符串进行拼接
使用concat_ws()可指定连接符进行拼接,第一个参数是连接符
select concat("姓名->",name," ","性别->",gender) from student; # 合并成了一个字符串,注意用的空格分隔开的,不然会黏在一起 +--------------------------------------------------+ | concat("姓名->",name," ","性别->",gender) | +--------------------------------------------------+ | 姓名->yunya 性别->male | | 姓名->jack 性别->male | | 姓名->bella 性别->famale | | 姓名->dairis 性别->famale | | 姓名->kyle 性别->famale | | 姓名->alice 性别->famale | | 姓名->ken 性别->male | | 姓名->jason 性别->male | | 姓名->tom 性别->male | | 姓名->fiona 性别->famale | +--------------------------------------------------+
select concat_ws("|||",name,gender,age) from student; # 使用|||为每个字段进行分割 +----------------------------------+ | concat_ws("|||",name,gender,age) | +----------------------------------+ | yunya|||male|||18 | | jack|||male|||17 | | bella|||famale|||17 | | dairis|||famale|||18 | | kyle|||famale|||19 | | alice|||famale|||16 | | ken|||male|||16 | | jason|||male|||21 | | tom|||male|||20 | | fiona|||famale|||19 | +----------------------------------+
5、where
where条件是查询的第一道坎,能有效过滤出我们想要的任意数据
5.1、比较运算
使用比较运算符> < >= <= !=进行查询
以下示例将展示使用where过滤出js成绩大于80分的同学
select name, js from student where js > 80; +-------+----+ | name | js | +-------+----+ | jack | 88 | | bella | 91 | | jason | 90 | | tom | 91 | +-------+----+
5.2、逻辑运算
使用and or not可进行逻辑运算与多条件查询
以下示例将展示使用where多条件查询过滤出各科成绩都大于80分的同学
select name, html, css, js from student where html > 80 and css > 80 and js > 80; +-------+------+-----+----+ | name | html | css | js | +-------+------+-----+----+ | jack | 92 | 81 | 88 | | jason | 91 | 92 | 90 | +-------+------+-----+----+
5.3、成员运算
in可以在特定的值中进行获取,如in(80,90,100)则代表只取80或者90或者100的这几条记录。
以下示例将展示只取第一组first以及第二组second学生的个人信息
select name, gender, age, hoc_group from student where hoc_group in ("first","second"); +-------+--------+-----+-----------+ | name | gender | age | hoc_group | +-------+--------+-----+-----------+ | yunya | male | 18 | first | | jack | male | 17 | second | | bella | famale | 17 | first | | alice | famale | 16 | second | +-------+--------+-----+-----------+
5.4、between and
between and也是取区间的意思,
以下示例将展示使用between and过滤出js成绩大于等于60并且小于80的同学
select name, js from student where js between 60 and 80; +-------+----+ | name | js | +-------+----+ | yunya | 76 | | kyle | 60 | +-------+----+
5.5、like
like是模糊查询,其中%代表任意多个字符(类似于贪婪匹配的通配符.*),_代表任意一个字符(类似于非贪婪匹配的通配符.*?)。
以下示例将展示使用like/%匹配出姓名以k开头的所有同学的名字
select name from student where name like "k%"; +------+ | name | +------+ | kyle | | ken | +------+
以下示例将展示使用like/_匹配出姓名以k开头并整体长度为3的同学的名字
select name from student where name like "k__"; +------+ | name | +------+ | ken | +------+
5.6、正则匹配
使用regexp可进行正则匹配,以下示例将展示使用正则匹配出名字中带有k的所有同学姓名
select name from student where name regexp "k+"; +------+ | name | +------+ | jack | | kyle | | ken | +------+
6、group by
分组行为发生在where条件之后,我们可以将查询到的记录按照某个相同字段进行归类,一般分组都会配合聚合函数进行使用。
需要注意的是select语句是排在group by条件之后的,因此聚合函数也能在select语句中使用。
6.1、基本使用
以下示例将展示对hoc_group字段进行分组。
我们按照hoc_group字段进行分组,那么select查询的字段只能是hoc_group字段,想要获取组内的其他字段相关信息,需要借助函数来完成
select hoc_group from student group by hoc_group; +-----------+ | hoc_group | +-----------+ | fifth | | first | | fourth | | second | | third | +-----------+
如果不使用分组,则会产生重复的信息
mysql> select hoc_group from student; +-----------+ | hoc_group | +-----------+ | first | | second | | first | | third | | fifth | | second | | third | | fourth | | fifth | | fourth | +-----------+
6.2、group_concat
用什么字段名进行分组,在select查询时就只能查那个用于分组的字段,查询别的字段会抛出异常,会提示sql_mode异常。
我们将js成绩大于80分的同学筛选出来并且按照gender字段进行分组,此外我们还想查看其所有满足条件同学的名字。
以下这样操作会抛出异常。
mysql> select gender,name from student where js > 80 group by gender; error 1055 (42000): expression #1 of select list is not in group by clause and contains nonaggregated column 'school.student.name' which is not functionally dependent on columns in group by clause; this is incompatible with sql_mode=only_full_group_by
必须借助group_concat()函数来进行操作才能使我们的需求圆满完成。
select gender, group_concat(name) from student where js > 80 group by gender; +--------+--------------------+ | gender | group_concat(name) | +--------+--------------------+ | male | jack,jason,tom | | famale | bella | +--------+--------------------+
6.3、分组模式
only_full_group_by要求select中的字段是在与group by中使用的字段
如果group by是主键或unique not null时可以在select中列出其他字段
#查看mysql 5.7默认的sql_mode如下: mysql> select @@global.sql_mode; only_full_group_by,strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution #设置sql_mole如下操作(我们可以去掉only_full_group_by模式): mysql> set global sql_mode='strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution';
6.4、聚合函数
聚合函数可以在where执行后的所有语句中使用,比如having,select等。
聚合函数一般是同分组进行配套使用,以下是常用的聚合函数。
函数名 | 作用 |
---|---|
count() | 对组内成员某一字段求个数 |
max() | 对组内成员某一字段求最大值 |
min() | 对组内成员某一字段求最小值 |
avg() | 对组内成员某一字段求平均值 |
sum() | 对组内成员某一字段求和 |
注意:不使用分组,则默认为一组 |
以下示例将展示求每组的成绩总和
select hoc_group, sum(js+html+css) from student group by hoc_group; +-----------+-------------------+ | hoc_group | sum(js+html+css) | +-----------+-------------------+ | fifth | 366 | | first | 488 | | fourth | 449 | | second | 391 | | third | 298 | +-----------+-------------------+
以下示例将展示整个班级的平均成绩及总成绩(round()用于四舍五入操作)
select round(avg(html+js+css)) as 平均分 ,sum(html+js+css) as 总分 from student; +-----------+--------+ | 平均分 | 总分 | +-----------+--------+ | 199 | 1992 | +-----------+--------+
以下示例将展示打印出总科成绩最高分数
select max(js+css+html) from student; +------------------+ | max(js+css+html) | +------------------+ | 273 | +------------------+
以下示例将展示查看本班有多少男生,多少女生
select gender, count(id) from student group by gender; +--------+-----------+ | gender | count(id) | +--------+-----------+ | male | 5 | | famale | 5 | +--------+-----------+
7、having
having也可用于过滤操作
7.1、区别差异
执行优先级从高到低:where> group by > having
where发生在分组group by之前,因而where中可以有任意字段,但是绝对不能使用聚合函数。
having发生在分组group by之后,因而having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
7.2、示例演示
以下示例将展示使用having过滤取出每组总分数大于400的小组
select hoc_group, sum(html+css+js) from student group by hoc_group having sum(html+css+js) > 400; +-----------+------------------+ | hoc_group | sum(html+css+js) | +-----------+------------------+ | first | 488 | | fourth | 449 | +-----------+------------------+
以下示例将展示使用having过滤取出有处分的同学。(可以使用分组的字段,但不能使用其他字段)
select sanction, group_concat(name) from student group by sanction having sanction != "无"; +-----------+--------------------+ | sanction | group_concat(name) | +-----------+--------------------+ | 大处分 | dairis,kyle,ken | | 小处分 | bella | +-----------+--------------------+
8、ordery by
ordery by用于对查询结果进行排序
默认的排序是按照主键进行排序的
8.1 asc
asc用于升序排列,以下示例将展示按照每位同学的年龄进行升序排列,如果年龄相同则依照总成绩进行升序排列。
select id, name, age, html+css+js as 总成绩 from student order by age, html+css+js asc; +----+--------+-----+-----------+ | id | name | age | 总成绩 | +----+--------+-----+-----------+ | 7 | ken | 16 | 112 | | 6 | alice | 16 | 130 | | 3 | bella | 17 | 231 | | 2 | jack | 17 | 261 | | 4 | dairis | 18 | 186 | | 1 | yunya | 18 | 257 | | 5 | kyle | 19 | 115 | | 10 | fiona | 19 | 176 | | 9 | tom | 20 | 251 | | 8 | jason | 21 | 273 | +----+--------+-----+-----------+
8.2、desc
desc用于降序排列,以下示例将展示按照每位同学的年龄进行降序排列。
select id, name, age, html+css+js as 总成绩 from student order by age desc; +----+--------+-----+-----------+ | id | name | age | 总成绩 | +----+--------+-----+-----------+ | 8 | jason | 21 | 273 | | 9 | tom | 20 | 251 | | 5 | kyle | 19 | 115 | | 10 | fiona | 19 | 176 | | 1 | yunya | 18 | 257 | | 4 | dairis | 18 | 186 | | 2 | jack | 17 | 261 | | 3 | bella | 17 | 231 | | 6 | alice | 16 | 130 | | 7 | ken | 16 | 112 | +----+--------+-----+-----------+
9、limit
limit用于控制显示的条数
9.1、示例演示
按照总成绩进行降序排序,只打印1-5名。
select id, name, age, html+css+js as 总成绩 from student order by html+css+js desc limit 5; +----+-------+-----+-----------+ | id | name | age | 总成绩 | +----+-------+-----+-----------+ | 8 | jason | 21 | 273 | | 2 | jack | 17 | 261 | | 1 | yunya | 18 | 257 | | 9 | tom | 20 | 251 | | 3 | bella | 17 | 231 | +----+-------+-----+-----------+
按照总成绩进行降序排序,只打印6-8名。
select id, name, age, html+css+js as 总成绩 from student order by html+css+js desc limit 5,3; # 从第五名开始,打印三条。 6,7,8 +----+--------+-----+-----------+ | id | name | age | 总成绩 | +----+--------+-----+-----------+ | 4 | dairis | 18 | 186 | | 10 | fiona | 19 | 176 | | 6 | alice | 16 | 130 | +----+--------+-----+-----------+
总结
到此这篇关于mysql单表查询的文章就介绍到这了,更多相关mysql单表查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Nextpeer 让单人游戏变成多人游戏