数据库-DQL

news/2025/3/12 12:23:26/

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 表名;

示例:

select name, entrydate from tb_emp;


select *
from tb_emp;

select name as 姓名, entrydate as 入职日期 from tb_emp;

select distinct job from tb_emp;

DQL-条件查询

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

示例:

select *
from tb_emp where name='杨逍';

select *
from tb_emp where id<=5;

select *
from tb_emp where job is null;

select *
from tb_emp where job is not null;

select *from tb_emp where password!=123456;

select *
from tb_emp
where entrydate between '2001-01-01' and '2010-01-01 'and gender = 2;

select *
from tb_emp
where job in (2, 3, 4);

select *
from tb_emp
where name like '__';

select *
from tb_emp
where name like '张%';

 DQL-分组查询

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

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

        函数:

                count:统计数量

                max:最大值

                min:最小值

                avg:平均值

                sum:  求和

示例:

select count(id)from tb_emp;
select count(0)from tb_emp;
select count(*)from tb_emp;

select min(entrydate)from tb_emp;

select avg(id)from tb_emp;

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

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

DQL-分组查询

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

select gender,count(*) from tb_emp group by gender;

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:降序

示例:

select *
from tb_emp order by entrydate;

select *
from tb_emp order by entrydate ,update_time desc ;

DQL-分页查询

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

示例:

select *
from tb_emp limit 0,5;

 注意:

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

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

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

案例:

select *
from tb_emp
where name like '张%'and gender = 1and entrydate between '2000-01-01' and '2015-01-01'
order by update_time desc
limit 0,10;

select (case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教研主管'else'未分配'end)职位,
count(*)
from tb_emp group by job;


http://www.ppmy.cn/news/1082418.html

相关文章

Linux CPU线程绑核

为了加快程序的运行速度和充分利用CPU资源&#xff0c;我们可以人为将不同线程绑定在不同的cup上&#xff0c;例如有两个线程A,B&#xff0c;其中A已经在CPU0上运行&#xff0c;并且CPU0上还有其他的任务&#xff0c;那么我们可以将线程B绑到CPU1上&#xff0c;这样就可以减轻C…

构造函数和析构函数(个人学习笔记黑马学习)

构造函数:主要作用在于创建对象时为对象的成员属性赋值&#xff0c;构造函数由编译器自动调用&#xff0c;无须手动调用。析构函数:主要作用在于对象销毁前系统自动调用&#xff0c;执行一些清理工作。 #include <iostream> using namespace std;//对象初始化和清理class…

VS + QT 封装带UI界面的DLL

一、创建编译DLL的项目 1.新建Qt Class Liabrary 2.新建项目&#xff0c;选择Qt Widgets Class 3.新建C类&#xff0c;可以在此类里面写算法函数用于调用。 4.下面是添加完Qt窗体类和C类之后的项目截图 5.修改头文件并编译 将uidemo_global.h中的ifdef内容复制到dialog.h上…

Ajax与jQuery

目录 Ajax是一种异步无刷新的技术 Ajax的优点&#xff1a; 可以无需刷新页面与服务器端进行通信允许根据用户事件来更新部分页面内容 Ajax的缺点&#xff1a; 没有浏览历史&#xff0c;不能回退存在跨域问题&#xff08;同源&#xff09;SEO&#xff08;搜索引擎优化&#x…

软考A计划-网络工程师-复习背熟-数据通信基础和局域网技术

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例点击跳转>软考全系列点击跳转>蓝桥系列 &#x1f449;关于作者 专注于Android/Unity和各种游…

NLP(六十七)BERT模型训练后动态量化(PTDQ)

本文将会介绍BERT模型训练后动态量化&#xff08;Post Training Dynamic Quantization&#xff0c;PTDQ&#xff09;。 量化 在深度学习中&#xff0c;量化&#xff08;Quantization&#xff09;指的是使用更少的bit来存储原本以浮点数存储的tensor&#xff0c;以及使用更少的…

【进阶篇】MySQL分库分表详解

文章目录 0. 前言1. 垂直分库分表2. 水平分库分表 1. 理解过程及实现方案问题讨论衍生出分库分表策略借助成熟组件使用分库分表阶段完成后面临的问题1. 异地多活问题2. 数据迁移问题3. 分布式事务问题4. join查询的问题 分库分表的策略实现示例 2. 参考文档 0. 前言 假设有一个…

【配置环境】Visual Studio 配置 OpenCV

目录 一&#xff0c;环境 二&#xff0c;下载和配置 OpenCV 三&#xff0c;创建一个 Visual Studio 项目 四&#xff0c;配置 Visual Studio 项目 五&#xff0c;编写并编译 OpenCV 程序 六&#xff0c;解决CMake编译OpenCV报的错误 一&#xff0c;环境 Windows 11 家庭中…