news 2026/5/25 6:22:20

MySQL:select查询语法大全

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
MySQL:select查询语法大全

本文详细介绍了MySQL的查询语法,并配有举例说明,请耐心观看。

额外说明:#在sql中代表注释符号。

下方是举例要使用的表和数据,可以根据自生情况进行更改。

drop database csdn; create database csdn; use csdn; create table student( id int primary key auto_increment, name varchar(20), age int, sex int, class_id int ); create table teacher( id int primary key auto_increment, name varchar(20), age int, sex int, class_id int ); create table class( id int primary key auto_increment, name varchar(20) ); # 由于篇幅问题,这里将插入数据写为了一行 # 插入班级数据 INSERT INTO class (id, name) VALUES(1, '一班'),(2, '二班'),(3, '三班'),(4, '四班'),(5, '五班'); # 插入学生数据 INSERT INTO student (id, name, age, sex, class_id) VALUES(1, '张三', 18, 1, 1),(2, '李四', 19, 1, 1),(3, '王五', 18, 0, 1),(4, '赵六', 20, 1, 1),(5, '钱七', 19, 0, 1),(6, '孙八', 18, 1, 2),(7, '周九', 19, 1, 2),(8, '吴十', 20, 0, 2),(9, '郑一', 18, 1, 2),(10, '刘二', 19, 0, 2),(11, '陈三', 18, 1, 3),(12, '杨四', 19, 1, 3),(13, '黄五', 20, 0, 3),(14, '周六', 18, 1, 3),(15, '林七', 19, 0, 3),(16, '何八', 18, 1, 4),(17, '高九', 19, 1, 4),(18, '罗十', 20, 0, 4),(19, '宋一', 18, 1, 4),(20, '谢二', 19, 0, 4),(21, '韩三', 18, 1, 5),(22, '唐四', 19, 1, 5),(23, '冯五', 20, 0, 5),(24, '邓六', 18, 1, 5),(25, '曹七', 19, 0, 5); # 插入教师数据 INSERT INTO teacher (id, name, age, sex, class_id) VALUES(1, '王老师', 35, 1, 1),(2, '李老师', 42, 0, 1),(3, '张老师', 38, 1, 2),(4, '赵老师', 45, 0, 2),(5, '刘老师', 40, 1, 3),(6, '陈老师', 36, 0, 3),(7, '杨老师', 43, 1, 4),(8, '黄老师', 39, 0, 4),(9, '周老师', 41, 1, 5),(10, '吴老师', 37, 0, 5),(11, '欧阳老师', 37, 0, 6);

1、基础语法

select 字段名1,字段名2 ...
from 表名; #查询该表中的具体某一个或几个字段

select * from 表名; #查询该表中的所有字段

select * from student; select id, name, sex from student;

2、关键字

2.1 as 和 distinct

2.1.1 as

as 后接自定义名称,用于给字段起别名,可以在展示时将原有字段名或表名替换为自定义名称

注意事项:

  1. 不是每个查询语句都需要取别名,一般按按实际需求来。
  2. as可以省略,字段名或表名在与别名之间用空格就行。
  3. 子查询作为from数据源时,必须要使用别名。
select id, name as username, sex gender from student;

2.1.1 distinct

distinct 用于给字段去重,distinct必须写在所有字段的前面,对后方所有字段进行组合去重

select distinct class_id from student ; select distinct class_id , id from student ; select id ,distinct class_id from student ;#本行sql会报错

2.2 where

where后接条件,用于筛选符合条件的数据

2.2.1 条件判断符号

符号作用
>大于
<小于
=等于
!=不等于
<>不等于
>=大于等于
<=小于等于
is null是否为null
is not null是否不为null
in在其中几个之一
between在两数之间

注意事项:

  1. =和!=,以及<>都不可以对null进行比较。
  2. null只能用is null和is not null进行比较。
  3. 日期类型也可以用上述符号进行比较。
  4. in和between前可以加not,作用与之相反
select * from student where id = 1; select * from student where name is not null; select * from student where id in (1,7,13); select * from student where id between 1 and 3;

2.2.2 like 模糊匹配

注意事项:

  1. %为匹配符,表示0个或多个字符串。
  2. %加在匹配词前,表示要匹配所有以匹配词为末尾的数据。
  3. %加在匹配词后,表示要匹配所有以匹配词为开头的数据。
  4. %在匹配词的前后都加,表示要匹配所有匹配词居中的数据。
  5. like前可加not关键字,表示匹配与匹配结果不同的数据
  6. 所有字段均可以匹配。
#匹配所有姓周的学生 select * from student where name like '周%';

2.2.3 正则匹配

符号作用
^匹配开头位置
$匹配末尾位置
.匹配除“\n”之外的任何单个字符
[...]匹配括号内的任意一个字符
[^...]匹配不在括号内的任意一个字符
(a1|a2|a3)匹配括号中的某一个字符串就行
*匹配前面的子表达式零次或多次
+匹配前面的子表达式至少一次
{n}匹配前面的子表达式n次
{n,}匹配前面的子表达式至少n次
{n,m}匹配前面的子表达式至少n次,最多m次

regexp和rlike是进行正则匹配的关键字,二者作用相同。

注意事项:

  1. ^加在正则表达式开头,$加在正则表达式的末尾。
  2. 若^和$都被加在正则表达式上,则只有数据严格等于正则表达式才会被匹配中。
  3. (a1|a2|a3)中的每一个匹配项都可以是正则表达式,且|优先级极高,在使用中,若还有其它的正则匹配规则,括号必不可少。
  4. {n}只匹配前一个表达式,建议将要匹配的子表达式用括号括起来。
#匹配所有姓周或姓王的学生 select * from student where name rlike '^[周王]';

2.2.4 合并条件符号

符号作用
and要同时满足and前后的条件
not不满足not后的条件
oror的前后条件之中,满足一个即可

注意事项:

  1. and除在between中使用外,和or都需要至少要两个条件才能使用,且放在两个条件之间。
  2. not可以在单个条件使用,放在该条件之前;也可以在多个条件中使用,放在条件之间。
#匹配所有姓周或姓王的学生 select * from student where name like '周%' or name like '王%'; #匹配所有不姓王的学生 select * from student where not name like '王%';

2.3 group by

group by 后接字段,用于分组

注意事项:

  1. group by 字段 后不可以接asc或desc,只能使用order by排序。
  2. group by 字段,字段,字段......group by后可以接多个字段进行分组,先根据第一个字段进行分组,再根据第一次分组的结果,每一小组再根据第二个字段进行分组,以此类推。

聚合函数:

公式作用
count(*)获取行数
max(列名)取最大数
min(列名)取最小数
sum(列名)求和
avg(列名)求平均值
group_concat(distinct 列名,列名...separator '间隔内容')将列与列拼接,distinct可加可不加,separator后的为行与行的间隔内容,可以不写separator,默认为逗号
select max( id),class_id from student group by class_id ; select class_id,group_concat(name) from student group by class_id ; select class_id,group_concat(name separator '*') from student group by class_id ;

2.4 having

having后接条件,用于展示分组数据。

注意事项:

  1. 只有使用了group by,才能使用having。
  2. having后接的条件,条件判断中的字段必须是select要查询出来的字段,
    如:select 字段1,max(字段2) 字段max ,count(*) size from 表1 group by 字段1 ;
    条件判断中的字段就只能在字段1、字段max和size中使用,
    除非使用聚合函数,才能使用表1中的其它字段。
select max(id) as max_id,class_id from student group by class_id having max_id > 1 ;

2.5 order by

order by后接字段,再后可接asc或desc。

注意事项:

  1. asc为升序,desc为降序,默认为asc。
  2. order by后也可以接多个字段,并使用asc或desc关键字,先按第一个字段排序,有相同排名的在使用第二个字段排序,以此类推。
  3. order by后接的条件,条件判断中的字段必须是select要查询出来的字段,
    如:select 字段1,字段2 , 字段3 from 表1 order by 字段1 ;
    条件判断中的字段就只能在字段1、字段max和size中使用。
select * from student order by id; select * from student order by id desc; select * from student order by id asc ;

2.6 limit

limit后接"索引,数量",也可以只接数量

注意事项:

  1. 索引是从0开始的,与id或其它自定义字段无关。
  2. 从索引处开始,取指定数量的数据。
select * from student limit 2;#取前两个 select * from student limit 2,2;#取第3第4个

3、联表查询

inner join 、left join 和 cross join 都可以互相叠加

3.1 inner join

表1 inner join 表2 on 条件 可以把两个表用特定的条件组合起来

注意事项:

  1. inner join 的 inner 可省略,直接写为 join 。
  2. 表1 inner join 表2 on 表1.字段1 = 表2.字段1
    与 form 表1,表2 where 表1.字段1 = 表2.字段1
    前者是显式内连接,后者式隐式内连接。
    作用一致,但显式内连接结构更加清晰。
    推荐使用 inner join 。
  3. inner join 可以叠加
    表1 inner join 表2 on 表1.字段1 = 表2.字段1 inner join 表3 on 表2.字段1 = 表3.字段1
select * from student inner join class on student.class_id = class.id;

3.2 left join

表1 left join 表2 on 条件
要返回表1的全部数据,表2匹配条件的数据就相应返回,没有与表1相匹配的数据就null

注意事项:

  1. left join 与 right join 的作用正好相反。
  2. left join 也可以叠加。
#id为11的teacher,没有对应的class select * from teacher left join class on teacher.class_id = class.id;

3.3 cross join

表1 cross join 表2 无需条件,返回两张表的笛卡尔积(所有组合)

select * from teacher cross join class ;

4、子查询

4.1 标量子查询

整个子查询只返回一个值

select max(id) from class; select * from student where id = (select max(id) from student);

4.2 列子查询

整个子查询只返回一列数据

select id from class; select * from student where class_id in (select id from class);

4.3 行子查询

整个子查询只返回一行数据

select class_id ,max( id) from student group by class_id order by class_id limit 1; select * from student where (id,class_id) = (select max(id) id ,class_id from student group by class_id order by class_id limit 1);

4.4 表子查询

一般返回多行多列,和正常查询差不多

select class_id ,max( id) from student group by class_id order by class_id; select * from student where (id,class_id) in (select max(id) id ,class_id from student group by class_id order by class_id );

4.5 子查询的注意事项

  1. 每一个子查询都是一个完整且独立的查询语句。
  2. 子查询可以做from的数据源,也可以做where中的判断数据。
  3. 但子查询在做from的数据源时,必须要起别名。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 1:41:37

ImageToSTL:解锁图片的第三个维度,让创意从平面走向立体

ImageToSTL&#xff1a;解锁图片的第三个维度&#xff0c;让创意从平面走向立体 【免费下载链接】ImageToSTL This tool allows you to easily convert any image into a 3D print-ready STL model. The surface of the model will display the image when illuminated from th…

作者头像 李华
网站建设 2026/5/23 1:41:41

如何快速掌握Open-Sora:AI视频创作的终极完整指南

如何快速掌握Open-Sora&#xff1a;AI视频创作的终极完整指南 【免费下载链接】Open-Sora Open-Sora: Democratizing Efficient Video Production for All 项目地址: https://gitcode.com/GitHub_Trending/op/Open-Sora 想象一下&#xff0c;只需几行文字描述&#xff0…

作者头像 李华
网站建设 2026/5/23 1:41:37

javaweb图书资料借阅信息管理系统的设计与实现

目录同行可拿货,招校园代理 ,本人源头供货商功能模块分析用户管理模块图书管理模块借阅管理模块逾期管理模块统计报表模块技术实现要点扩展功能建议项目技术支持源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作同行可拿货,招校园代理 ,本人源…

作者头像 李华
网站建设 2026/5/23 1:41:36

分子进化分析中的贝叶斯树重建:BEAST 2核心技术与实践指南

分子进化分析中的贝叶斯树重建&#xff1a;BEAST 2核心技术与实践指南 【免费下载链接】beast2 Bayesian Evolutionary Analysis by Sampling Trees 项目地址: https://gitcode.com/gh_mirrors/be/beast2 副标题&#xff1a;3大核心收益 掌握贝叶斯系统发育推断的数学原…

作者头像 李华
网站建设 2026/5/23 1:41:45

windows安装nacos教程

下载 访问官网下载压缩包&#xff0c;下载那个二进制的 https://nacos.io/download/nacos-server/ 创建数据库和修改一些配置 在mysql中创建一个叫nacos的数据库&#xff0c;nacos数据库中执行config目录下mysql-schema.sql文件 打开解压后的目录下的conf目录&#xff0c;再…

作者头像 李华