数据库-DQL

DQL:用来查询数据库表中的记录

关键字:SELECT

语法:

select:字段列表

from:表名列表

where:条件列表

group by:分组列表

having:分组后条件列表

order by:排序字段列表

limit:分页参数

DQL-基本查询

查询多个字段:select 字段1,字段2,字段3 from 表名;

查询所有字段(通配符):select*from 表名;

设置别名:select 字段1[as 别名1],字段2[as 别名] from 表名;

去除重复记录:select distinct 字段列表 from 表名;

示例:

sql 复制代码
select name, entrydate from tb_emp;
sql 复制代码
select *
from tb_emp;
sql 复制代码
select name as 姓名, entrydate as 入职日期 from tb_emp;
sql 复制代码
select distinct job from tb_emp;

DQL-条件查询

条件查询:select 字段列表 from where 条件列表;

示例:

sql 复制代码
select *
from tb_emp where name='杨逍';
sql 复制代码
select *
from tb_emp where id<=5;
sql 复制代码
select *
from tb_emp where job is null;
sql 复制代码
select *
from tb_emp where job is not null;
sql 复制代码
select *from tb_emp where password!=123456;
sql 复制代码
select *
from tb_emp
where entrydate between '2001-01-01' and '2010-01-01 '
  and gender = 2;
sql 复制代码
select *
from tb_emp
where job in (2, 3, 4);
sql 复制代码
select *
from tb_emp
where name like '__';
sql 复制代码
select *
from tb_emp
where name like '张%';

DQL-分组查询

**介绍:**将一列数据作为一个整体,进行纵向计算

语法:select聚合函数(字段列表)from 表名;

函数:

count:统计数量

max:最大值

min:最小值

avg:平均值

sum: 求和

示例:

sql 复制代码
select count(id)from tb_emp;
select count(0)from tb_emp;
select count(*)from tb_emp;
sql 复制代码
select min(entrydate)from tb_emp;
sql 复制代码
select avg(id)from tb_emp;

注意:null值不参与所有聚合函数运算

统计数量可以用:count(*) count(字段) count(常量) 推荐:count(*)

DQL-分组查询

select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件]

sql 复制代码
select gender,count(*) from tb_emp group by gender;
sql 复制代码
select job ,count(*) from tb_emp where entrydate<='2015-01-01' group by job having count(*)>=2;

where和having区别

1.执行时机不同:where是分组之前进行过滤,不满足where条件,不参与where条件,不参与分组;而having是分组之后对结果进行过滤

2.判断条件不同:where不能对聚合函数进行判断,而having可以

DQL-排序查询

条件查询:

select 字段列表 from 表名 [where 条件] [group by 分组字段]order by字段1 排序方式1,字段2 排序方式2;
ASC:升序(默认值)

DESC:降序

示例:

sql 复制代码
select *
from tb_emp order by entrydate;
sql 复制代码
select *
from tb_emp order by entrydate ,update_time desc ;

DQL-分页查询

select 字段列表 from 表名 limit 起始索引,查询记录数;

示例:

sql 复制代码
select *
from tb_emp limit 0,5;

注意:

1.起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数

2.分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMT

3.如果查询的是第一页数据,起始索引可以省略,直接简写为limit10

案例:

sql 复制代码
select *
from tb_emp
where name like '张%'
  and gender = 1
  and entrydate between '2000-01-01' and '2015-01-01'
order by update_time desc
limit 0,10;
sql 复制代码
select (case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教研主管'else'未分配'end)职位,
count(*)
from tb_emp group by job;
相关推荐
heimeiyingwang7 小时前
企业供应链 AI 优化:需求预测与智能调度
大数据·数据库·人工智能·机器学习
山岚的运维笔记8 小时前
SQL Server笔记 -- 第73章:排序/对行进行排序
数据库·笔记·后端·sql·microsoft·sqlserver
XLYcmy8 小时前
智能体大赛 目录
数据库·ai·llm·prompt·agent·检索·万方
盟接之桥8 小时前
盟接之桥EDI软件:API数据采集模块深度解析,打造企业数据协同新引擎
java·运维·服务器·网络·数据库·人工智能·制造
QQ24391979 小时前
语言在线考试与学习交流网页平台信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·spring boot·sql·学习·java-ee
闲人编程9 小时前
内存数据库性能调优
数据库·redis·字符串·高并发·哈希·内存碎片
l1t9 小时前
DeepSeek总结的PostgreSQL 19新功能:第一部分
数据库·postgresql
青衫码上行11 小时前
高频 SQL 50题(基础版)| 查询 + 连接
数据库·sql·学习·mysql
Anastasiozzzz12 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
iameyama12 小时前
python Pandas 开发
数据库