MySQL数据库-----多种查询方式详解(可收藏)

2025-06-19 10:26:40

本文主要讲述了MySQL数据库的多种查询方式,且MySQL语句是需要记忆的,但是有随时忘记的可能,如果读者喜欢我的文章的话可以选择收藏该文章,在你忘记的时候可以随时查看!

1、基本查询

查询所有字段:

select * from 表名; 查询指定字段:

方式一:select 列1, 列2,…… from 表名;方式二:select 表名.列1, 表名.列2,…… from 表名; 使用 as 给字段起别名:

select 字段 as 别名…… from 表名;哪列在前先出现哪列。 可以通过 as 给表起别名:

select 别名.列1, 别名.列2,…… from 表名 as 别名;注意:起了别名就要用别名,不能在用表名,否者出现错误!如下:

select 表名.列1, 表名.列2,…… from 表名 as 别名; 查询过程中的去重(消除重复行):

添加 distinct 字段 实现去重。查询字段中的所有存在的结果:select distinct 字段名 from 表名;

2、条件查询

select …… from 表名 where 条件;比较运算符:

大于>小于<大于等于>=小于等于<=不等于!=等于=(注意:只有一个等号) 逻辑运算符:

andornot 不在 xxxx 这个范围的例如:年龄在18到28之间的女性

select * from 表名 where age>18 and age<18 and gender=‘女’; 模糊查询:

模糊查询效率最低!方式一(like):

% 替换一个或者多个数据_ 替换一个数据例如:

查询姓名中以“小”开始的名字:

select * from 表名 where name(字段名) like “小%”; 查询有2个字的名字:

select * from 表名 where name(字段名) like “__”; 查询有3个字的名字:

select * from 表名 where name(字段名) like “___”; 查询含有“月”字的名字:

select * from 表名 where name(字段名) like “%月%”; 查询至少含有2个字的名字:

select * from 表名 where name(字段名) like “__%”; 方式二(rlike):

rlike 正则表达式查询以 周开始的名字:

select * from 表名 where name(字段名) rlike “^周.*”; 查询以 周开始,以伦结尾的名字:

select * from 表名 where name(字段名) rlike “^周.*伦$”; 范围查询:

其中 age 为表中的字段名!in (数据1, 数据2, ……)在一个非连续的范围内:

select * from 表名 where age in (18, 12, 34);

等价于===>select * from 表名 where age=18 or age=12 or age=34; not in (数据1, 数据2, ……) 不在非连续的范围之内:

select * from 表名 where age not in (18, 12, 34); between … and … 表示在一个连续的范围内:

select * from 表名 where age between 18 and 34;

等价于===>select * from 表名 where age>=18 and age<=34; not between … and … 表示不在一个连续的范围内:

select * from 表名 where age not between 18 and 34; 空判断:

is null 判断空:

select * from 表名 where age is null(NULL); is not null 判断非空:

select * from 表名 where age is not null(NULL);

3、排序

默认排序是按照主键排序!单列排序:

order by 字段asc 从小到大排列,即升序

默认是从小到大排列,故asc可省略! desc 从大到小排列,即降序

查询表中的数据,按照年龄从小到大排序

select * from 表名 order by asc; 查询年龄在18到34岁之间的男性,按照年龄从小到大排序

select * from 表名 where (age between 18 and 34) and gender=“男” order by age asc; 多列排序:

在单列排序的条件相同的条件下:order by 多个字段select * from 表名 (条件) order by 首字段 排列方式, 次字段 排列方式 ……;查询年龄在18到34之间的女性,身高从高到矮,如果身高一样的情况下按照年龄从小到大排列:

select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;

4、聚合函数

count 总数:

查询男性有多少人

select count(*) from 表名 where gender=“男”;取个别名:select count(*) as 男性人数 from 表名 where gender=“男”; max 最大值:

查询最大年龄:

select max(age) from 表名; min 最小值:

查询最小年龄:

select min(age) from 表名; sum 求和:

计算所有人的年龄总和:

select sum(age) from 表名; avg 平均值:

计算平均年龄:

select avg(age) from 表名;或者:select sum(age)/count(*) from 表名; round 四舍五入:

round(参数1, 参数2)其中参数1表示数据的结果,参数2表示保留的小数位数。计算平均年龄,保留两位小数:

select round(avg(age), 2) from 表名;

5、分组

一般开发过程中分组聚合一起使用!**group by **:

按照性别分组,查询所有性别:

select gender from 表名 group by gender; 与distinct 有很大区别如下:

按照性别分组,查询所有性别及其人数:

select gender, count(*) from 表名 group by gender; 计算男性个数:

select gender, count(*) from 表名 where gender=“男” group by gender; group_concat() 把每组的参数字段的数据列出来:

例如按照性别分组,并列出每组人员姓名:

select gender, group_concat(name) from 表名 group by gender; 例如按照性别分组,并列出每组人员姓名、年龄、id:

select gender, group_concat(name,age,id) from 表名 group by gender;但是会出现数据直接相连接,无法读取数据**因此**数据之间加上下划线"_"

select gender, group_concat(name, “", age, "”, id) from 表名 group by gender; having 对分组后的数据进行条件判断:

where 和 having 的区别:

where 对元素表进行条件having 对分组后的结果,进行条件 查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30;

select gender, group_concat(name) from 表名 group by gender having avg(age) > 30;

6、分页查询

limit count 限制本次查询出来最多的数据个数:

select * from students limit 3; limit start, count:

start 代表从第几个数据开始

其中0 代表第一个数据 count 代表限制本次查询出来最多的数据个数查询id 3到5 的数据:

select * from students limit 2, 3; 查询页数的使用:

每页显示x个,第1页:

select * from 表名 limit 0, x; 每页显示x个,第2页:

select * from 表名 limit (2-1)*x, x; 每页显示x个,第3页:

select * from 表名 limit (3-1)*x, x; 每页显示x个,第4页:

select * from 表名 limit (4-1)*x, x; 总结:每页显示x个,第n页:

select * from 表名 limit (n-1)*x, x; 每页显示2个,显示第6页信息,按照年龄从小到大排列:

错误:select * from 表名 limit 10, 2 order by age asc

原因:limit 在最后!

7、链接查询

当查询结果的列表来源于多张表时,需要将多张表连接成一个大的数据库,在选择合适的列返回。mysql 支持三种类型的链接查询:

inner 内连接查询:

查询的结果为两个表中匹配到的数据。 left 左链接查询:

查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据用null补充。 right 右链接查询:

查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据用null补充。 左、右链接查询只需会一种即可,因为可以通过调换两表的位置来实现右链接! 内连接查询:

表A inner join 表B on 条件;查询 students表中 有能够对应classes表中班级 的学生信息及班级信息:

select * from students inner join classes on students.cls_id=classes.id; 左链接查询:

表A left join 表B on 条件;左表特有的数据,对于右表中不存在的数据用null补充。查询 每位学生对应的班级信息,不存在班级的用null表示:

select * from students left join classes on students.cls_id=classes.id; 查询 没有对应班级的学生:得到新表的结果,用havingwhere也可行,但不推荐使用!

select * from students left join classes on students.cls_id=classes.id having classes.id is null; 右链接查询:

表A right join 表B on 条件;右表特有的数据,对于左表中不存在的数据用null补充。

8、自关联

一个表按成两个表链接:

select * from 表名 as 别名1 inner join 表名 as 别名2 on 条件;

9、子查询

一个select中嵌套一个select!标量子查询:

查询最高的男生信息:

select * from students where height=(select max(height) from students); 列级子查询:

查询学生的班级号能够对应学生信息:

select * from students where cls_id in (select * from classes);

10、合并查询结果

union all 关键字可以将多条select语句组合成一个结果集。select id, name from students where id<10 union all select id, name from students where id in (1, 4, 7, 9);

上述查询语句等价于:select id, name from students where id<10 and id in (1, 4, 7, 9);

实例练习:

查询每种性别中年龄最大者的所有信息:即用到链接查询,又用到了子查询。

select *

from (select gender, max(age) as max_age

from students group by gender)as a

left join students as b

on (a.gender=b.gender and a.max_age=b.age);

但是:如果同一个性别中的最大年龄者有两个人或者多人时候,得到的数据位置可能不理想。可能结果如下(注意看存在两个男性):

性别年龄姓名男性38小明女性36小红保密40郭靖男性38林博伦两性20XXX

因此需要对该结果进行排序:

select *

from (select gender, max(age) as max_age

from students group by gender)as a

left join students as b

on (a.gender=b.gender and a.max_age=b.age)

order by a.gender asc;

性别年龄姓名保密40郭靖两性20XXX男性38林博伦男性38小明女性36小红

ps两个多个钢笔路径怎么合并转换为选区
如何轻松找回你曾经的微信头像?5个实用技巧助你找回旧图片