mysql的DQL语言

查询语法

基础查询

1.查询多个字段

select 字段列表 from 表名;

select * from 表名;--查询所有数据

sql 复制代码
insert into people (id,name,sex) values(1,'小明','男'),(2,'小红','女'),(3,'小李','男');
insert into people (id,name,sex) values(3,'小龙','男'),(4,'小飞','男');
update people set id=4 where name='小龙';
update people set id=5 where name='小飞';
 select id,name from people;-- 查询id 和name字段;
sql 复制代码
select * from people;-- 查询所有字段

2.去除重复记录

select distinct 字段名1,字段名2,... from 表名;

去除字段相同的行,只留一行;

sql 复制代码
insert into people(id,name ,sex) values(5,'小飞','女');

-- 去重
select distinct id,name from people;-- 去除id,name一样的行,不管sex是否相同

可见,已经去重

3.查询时给列,表指定别名需要使用AS关键字

select 字段名1 AS 别名,字段名2 AS 别名 ...from 表名;

select 字段名1 AS别名,字段名2 AS 别名 ... from 表名 AS 别名;

sql 复制代码
select name AS "姓名", sex AS "性别" from people ;

条件查询

1.条件基础查询

select 字段列表 from 表名 where 条件列表;

sql 复制代码
create table student
(
    id int,
    name varchar(10),
    age int,
    math int,
    english int
);
insert into student values(1,'小李',18,90,80),(2,'小红',20,59,78),(3,'小飞',22,100,57);
select * from student;

-- 查找数学大于80的人
select id, name, age, math, english from student where math>=80;

-- 查找英语小于60的人
select id,name,age,math,student.english from student where  english<60;#注意,不会查询到english数值为null的学生

-- 查找年龄不是20的人
select id, name, age, math, english from student where age!=20;
select id, name, age, math, english from student where age<>20;


-- 查找年龄大于18岁,且数学大于60分的学生
select id, name, age, math, english from student where age>18 and math>60;
select id, name, age, math, english from student where age>18 && math>60;

-- 查找 年龄大于18,或者数学大于60分的学生
select id, name, age, math, english
from student
where age>18 or math>60;
select id, name, age, math, english
from student
where age>18 || math>60;

-- 查找id 为1,3的学生
select id, name, age, math, english from student where id in(1,3);
select id, name, age, math, english from student where id=1 or id=3;

2.范围查询

between 值1 and 值2 ---表示从值1到值2的范围,包头又包尾

例如: age between 80 and 100;

相当于 age>=80 and age<=100;

sql 复制代码
-- 查找数学在80到100的范围
select id, name, age, math, english from student where math between 80 and 100;

3.模糊查询

select 字段列表 from 表名 where 字段名 like 模糊查询的关键字(关键字通常和通配符号关联)

通配符:

%:表示任意个数据

_:表示任意一个数据

sql 复制代码
-- 查找姓何的所有学生
select id, name, age, math, english from student where name like '何%';

-- 查找名字包含美字的学生
select id, name, age, math, english from student where name like '%美%';

-- 查找名字是马**的学生
select id, name, age, math, english from student where name like '马_';

4.排序查询

select 字段列表 from 表名 order by 字段名1 排序规则(升序或者降序),字段2 排序规则...

不写where

升序:ASC(默认,可以不写)

降序:DESC

注意:如果又多个排序条件,当前边的条件值一样时,才会根据第二条件进行排序

sql 复制代码
-- 以年龄升序排序
select id, name, age, math, english from student  order by age ASC;

-- 以年龄降序,如果年龄相同以数学成绩降序
select id, name, age, math, english from student order by age DESC,math DESC;
相关推荐
宸津-代码粉碎机1 小时前
OpenAI 连夜迎战 Grok Bot 和 Muse:AI 智能体从 “会聊天” 到 “能办事”,现在入场还来得及吗
java·大数据·人工智能·分布式·python
weixin_511255212 小时前
MySQL升级记录
数据库·mysql
chenbingjie_c2 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
噢,我明白了3 小时前
java中唯一键和幂等键的应用
java·后端
程序猿乐锅3 小时前
【黑马点评 | 第八篇】Redisson分布式锁
java·数据库·spring boot·redis·分布式·spring·缓存
Leo.yuan3 小时前
2026年本地化Data Agent优质厂商盘点:哪些产品更适合企业生产环境
大数据·数据库·人工智能
考虑考虑3 小时前
synchronized字符串常量
java·后端·java ee
长谷深风1113 小时前
Tool与Skill:AI能力设计的分水岭
java·人工智能·ai·大模型·aiagent
m0_587383003 小时前
广州24小时自助健身房解决方案实战指南与系统部署要点
java·spring·小程序·架构·需求分析
做运维的阿瑞3 小时前
数据库增删改的安全写法
数据库·sql·mysql