当前位置: 首页 > news >正文

查询数据库DQL

 DQL 查询基本语法

--  ======================DQL :基本语法==================;
-- 1查询指定的字段 name  entrydate  并返回select name , entrydate from tb_emp;-- 2 查询 所有字段  并返回select id, username, password, name, gender, image, job, entrydate, create_time, update_time from  tb_emp;-- 2 查询 所有字段  并返回select * from  tb_emp;
-- 3 查询 所有员工的 name entrydate 并起别名(姓名 入职日期)
select name as'姓名' , entrydate as'入职日期' from  tb_emp;
-- 缩写
select name '姓 名' , entrydate '入职日期' from  tb_emp;-- 查询已有的员工关联了那几种职位(不要重复)
select  distinct job from tb_emp

DQL 条件查询

-- ===============DQL:条件查询 ======================-- 1查询  姓名为  杨逍的 员工select * from tb_emp where name = '杨逍';--  2查询 id 小于5的 员工消息
select * from tb_emp where  id<5;-- 3查询 没有分配职位的员工的信息select * from tb_emp where  job is null;-- 4查询 有职位的 员工信息select * from tb_emp where  job  is not null ;-- 5查询 密码不等于 "123456" 的员工 信息select * from tb_emp  where password != 123456;--  一样select * from tb_emp  where password <> 123456;-- 6 查询 入职日期 2000-01 -01 (包含)到 '2000-01-01'之间的员工信息select * from tb_emp where  entrydate >= '2000-01-01' and entrydate<='2010-01-01';-- 缩写select * from tb_emp where  entrydate between '2000-01-01' and '2010-01-01';-- 7 查询 入职日期 2000-01 -01 (包含)到 '2000-01-01' 之间 且 性别是女的员工信息
select * from tb_emp where  entrydate between '2000-01-01' and '2010-01-01' and  gender=2;-- 一样
select * from tb_emp where  entrydate between '2000-01-01' and '2010-01-01' &&  gender=2;-- 8  查询 职位是2 (讲师) 3 (学工主管) 4 (教研) 员工信息select  * from tb_emp  where  job=2 or job=3 or job =4 ;-- 一样select  * from tb_emp  where  job in (2,3,4) ;select  * from tb_emp  where  job in (4) ;-- 9 查询 姓名 为两字的员工信息select * from  tb_emp where name like '____';select * from  tb_emp where name like '__';-- 10 查询 性名 '张' 的员工select  * from  tb_emp where  name like '张%';

 DQL分组查询  group by

-- =================DQL :分组查询 ======================-- 集合 函数
-- 1 统计该企业的员工信息 --count-- A  count 字段select count(id) from tb_emp;select count(name) from tb_emp;select count(job) from tb_emp;-- B count (常量)select count(0) from tb_emp;-- C count (count *)select  count(*) from tb_emp;-- 2 统计该企业最早入职的员工 --minselect  min(entrydate) from tb_emp;-- 3 统计该企业的最迟入职的员工 -- maxselect max(entrydate)from tb_emp;-- 4 统计该企业的 id 平均 值 - avgselect avg(id) from tb_emp;-- 5 统计该企业的 员工的ID 之和 - sumselect sum(id) from tb_emp;

分组查询 高级

 -- ============DQL:分组 查询 ===============-- 1   根据性别分组  统计男性 和女性员工的数量select gender , count(*) from tb_emp group by  gender;-- 2 先查询 入职日期 在 2015-01-10(包含 ) 以前的 员工  并对结果根据 职位分组  获取员工数量 大=大于等于2的职位select job , count(*) from tb_emp  where   entrydate <= '2015-01-10' group by  job having  count(*)>=2;

DQL排序查询 

 -- ================排序查询=========-- 1 根据入职日期 对员工 进行升序排序 --ascselect * from tb_emp order by  entrydate asc;select * from tb_emp order by  entrydate ;-- 2 根据 入职日期   对员工进行降序排序select * from tb_emp order by entrydate desc;-- 3 根据 入职日期 对公司员工进行 升序排序 入职时间相同的 再按照 更新时间 进行降序排序
select * from tb_emp order by  entrydate ,update_time desc ;

DQL 分页查询  limit

 -- =================分页查询 ===============;-- 1 从 起始索引0 开始查询员工数据  每页展示5 条记录select * from tb_emp   limit 0,5;-- 2 查询第1页 员工数据  每页展示5条记录select * from tb_emp   limit 0,5;-- 3 查询第2页 员工数据  每页展示5条记录
select * from tb_emp   limit 5,5;-- 4 查询第3页 员工数据  每页展示5条记录
select * from tb_emp   limit 10,5 ;-- 起始索引 =(页码 -1 *  每页展示的记录数 )
-- 起始索引 =(页码 -1 *  5 )



-- 案例 1 按照需求完成 员工管理 的条件分页 查询 - 根据 输入条件 查询第一页的数据 每一页展示 10条数据
-- 输入 条件
-- 姓名 :张
-- 性别 :男
-- 入职时间 : 2000-01-01 2015-12-31
select * from tb_emp where name like '%张%' and gender = 1 and entrydate between
'2000-01-01' and '2015-12-31'order by update_time desc limit 10,10;


-- 案例 2-1 :根据需求 完成员工职位信息的统计 count(*) if判断
select if (gender=1,'男性','女性')'性别', gender, count(*) from tb_emp group by gender ;

-- 2-2 根据需求 完成员工职位信息的统计 -- case

select job,count(*) from tb_emp group by job;
-- case job when 1 then'老师' hen 1 then'老师'hen 1 then'老师'

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

多表设计 

  1. 一对多

  2. 一对一 

  3. 多对多 

1.一对多 

create table tb_emp
(id          int auto_increment comment '主键ID'primary key,username    varchar(20)                  not null comment '用户名',password    varchar(32) default '123456' null comment '密码',name        varchar(10)                  not null comment '姓名',gender      tinyint unsigned             null comment '性别 1男 2女',image       varchar(300)                 null comment '图形url',job         tinyint unsigned             null comment '职位 ,1班主任,2 讲师 ,3 学工主管 ,4 教研主管 ',entrydate   date                         null comment '入职日期',dept_id int unsigned comment '归属部门',crete_time  datetime                     not null comment '创建时间',update_time datetime                     not null comment '修改时间',constraint usernameunique (username)
)comment '员工';-- 部门管理-- create  database  db_03;--  use db_03;
create  table  tb_dpt(id  int unsigned primary key  auto_increment comment 'ID',name varchar(10) not null  unique  comment '部门名称',creation_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间')comment '部门表';

alter table tb_emp  -- (用户表)add constraint tb_emp_fk_dept_id  -- 外连接名称foreign key (dept_id) references  -- 外连接创建表tb_dpt (id);   -- (部门表·)

2.一对一

create table  tb_user(id int unsigned primary key  auto_increment comment 'Id',name varchar(20) not null  comment '姓名',gender tinyint unsigned   not null comment '性别 1:女 2:男',phone char(11)comment '手机号',degree varchar (15) comment '学历') comment '用户表';
insert into tb_user values (1,'买买提',1,'18383905487','本科'), (2,'张小',1,'67124808247','大专'),(3,'李强',1,'18383905487','博士'), (4,'李方',2,'18383905487','初中') ;
create  table tb_user_card(id int unsigned primary key auto_increment comment 'id',ethnic_group varchar(10) not null  comment '民族',birthday date not null comment '生日',idcard char(18) not null comment '身份证号',issued varchar(20) not null comment '签发机关',expire_begin date comment '有效期限-开始时',expire_end date comment '有效期限-结束',user_id int unsigned not null unique comment '用户ID',constraint  fk_user_id foreign key (user_id) references tb_user(id)
)comment '用户信息表';insert into tb_user_card values(1,'汉','1998-08-14','453234199808147439','南京鼓励广场','2000-12-1',null,1),(2,'汉','1998-08-14','453234199808147439','南京鼓励广场','2000-12-1','2023-12-31',2),(3,'汉','1998-08-14','453234199808147439','南京鼓励广场','2000-12-1','2028-10-30',3),(4,'回','1998-08-14','453234199808147439','南京鼓励广场','2000-12-1',null,4)

多对多 

-- 多对多
-- 学生表
create table  tb_student(id int auto_increment primary key comment '主键ID',name varchar(10)comment '姓名',no varchar(10)comment '学号'
)comment '学生表';
insert into tb_student(name, no) values ('person1','20001'),('person2','20002'),('person3','20003'),('person4','20004');create table tb_course(id int auto_increment primary key comment '主键id',name varchar(10) comment '课程名称')comment '课程表' ;
insert into tb_course(name) values ('java'),('php'),('mysql'),('go'),('c'),('linux');create table  tb_student_course(id int unsigned auto_increment comment '主键' primary key ,student_id int not null comment '学生id',course_id int not null comment '课程id',constraint fk_courseid foreign key (course_id)references tb_course(id) ,constraint fk_studentid foreign key (student_id)references tb_student(id))comment '学生和课程表';insert into tb_student_course (student_id, course_id)values (1,1),(1,2),(1,3),(2,2),(2,3),(3,3);

 

相关文章:

查询数据库DQL

DQL 查询基本语法 -- DQL :基本语法; -- 1查询指定的字段 name entrydate 并返回select name , entrydate from tb_emp;-- 2 查询 所有字段 并返回select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp;-- 2 查询…...

SpringBoot中文乱码问题解决方案

在Spring Boot中&#xff0c;确实没有像传统Web应用程序中需要使用web.xml配置文件。对于中文乱码问题&#xff0c;你可以采取以下几种方式来解决&#xff1a; 在application.properties文件中添加以下配置&#xff1a; spring.http.encoding.charsetUTF-8 spring.http.encod…...

京东联盟flutter插件使用方法

目录 1.京东联盟官网注册申请步骤略~2.安卓端插件配置&#xff1a;3.IOS端插件配置4.其它配置5.京东OAuth授权 文档地址&#xff1a;https://baiyuliang.blog.csdn.net/article/details/134444104 京东联盟flutter插件地址&#xff1a;https://pub.dev/packages/jdkit 1.京东联…...

python电影数据可视化分析系统的设计与实现【附源码】

电影数据可视化分析系统的设计与实现 (一)开题报告&#xff0c;就是确定设计(论文)选题之后&#xff0c;学生在调查研究的基础上撰写的研究计划&#xff0c;主要说明设计(论文)研究目的和意义、研究的条件以及如何开展研究等问题&#xff0c;也可以说是对设计(论文)的论证和设…...

SQLMAP --TAMPER的编写

跟着师傅的文章进行学习 sqlmap之tamper脚本编写_sqlmap tamper编写-CSDN博客 这里学习一下tamper的编写 这里的tamper 其实就是多个绕过waf的插件 通过编写tamper 我们可以学会 在不同过滤下 执行sql注入 我们首先了解一下 tamper的结构 这里我们首先看一个最简单的例子…...

美国服务器:全面剖析其主要优点与潜在缺点

​  服务器是网站搭建的灵魂。信息化的今天&#xff0c;我们仍需要它来为网站和应用程序提供稳定的运行环境。而美国作为全球信息技术靠前的国家之一&#xff0c;其服务器市场备受关注。那么&#xff0c;美国服务器究竟有哪些主要优点和潜在缺点呢? 优点 数据中心基础设施&a…...

验证二叉搜索树

二叉搜索树 二叉查找树&#xff08;Binary Search Tree&#xff09;&#xff0c;&#xff08;又&#xff1a;二叉搜索树&#xff0c;二叉排序树&#xff09;它或者是一棵空树&#xff0c;或者是具有下列性质的二叉树&#xff1a; 若它的左子树不空&#xff0c;则左子树上所有结…...

Ubuntu 18.04无网络连接的n种可能办法

文章目录 网络图标消失&#xff0c;Ubuntu无网络连接VMware上Ubuntu18.04&#xff0c;桥接了多个网卡&#xff0c;其中一个用来上网&#xff0c;均设置为静态ip网络桥接链路没有接对路由不对 树莓派同时使用无线网卡和有线网卡&#xff0c;且一个连接内部局域网&#xff0c;一个…...

MIUI查看当前手机电池容量

MIUI查看当前手机电池容量 1. 按如下步骤操作生成bug报告 2. 按如下操作解压bug报告 Last learned battery capacity...

链动2+1模式:创新营销引领白酒产业新潮流

在当今高度竞争的市场环境中&#xff0c;创新营销模式对于企业的发展至关重要。链动21模式作为一种独特的营销策略&#xff0c;将白酒产品与该模式相结合&#xff0c;充分发挥其优势&#xff0c;通过独特的身份晋升和奖励机制&#xff0c;快速建立销售渠道&#xff0c;提高用户…...

openGauss学习笔记-126 openGauss 数据库管理-设置账本数据库-归档账本数据库

文章目录 openGauss学习笔记-126 openGauss 数据库管理-设置账本数据库-归档账本数据库126.1 前提条件126.2 背景信息126.3 操作步骤 openGauss学习笔记-126 openGauss 数据库管理-设置账本数据库-归档账本数据库 126.1 前提条件 系统中需要有审计管理员或者具有审计管理员权…...

UE 视差材质 学习笔记

视差材质节点&#xff1a; 第一个是高度图&#xff0c; Heightmap Channel就是高度图的灰色通道&#xff0c;在RGBA哪个上面&#xff0c;例如在R上就连接(1,0,0,0)&#xff0c;G上就连接&#xff08;0,1,0,0&#xff09;逐次类推 去看看对比效果&#xff1a; 这个是有视差效果…...

openfeign整合sentinel出现异常

版本兼容的解决办法&#xff1a;在为userClient注入feign的接口类型时&#xff0c;添加Lazy注解。 Lazy注解是Spring Framework中的一个注解&#xff0c;它通常用于标记Bean的延迟初始化。当一个Bean被标记为Lazy时&#xff0c;Spring容器在启动时不会立即初始化这个Bean&…...

Java的继承

继承(Inheritance) 【1】类是对对象的抽象&#xff1a; 举例&#xff1a; 荣耀20 &#xff0c;小米 红米3&#xff0c;华为 p40 pro ---> 类&#xff1a;手机类 【2】继承是对类的抽象&#xff1a; 举例&#xff1a; 学生类&#xff1a;Student&#xff1a; 属性&…...

十二、Docker的简介

目录 一、介绍 Docker 主要由以下三个部分组成&#xff1a; Docker 有许多优点&#xff0c;包括&#xff1a; 二、Docker和虚拟机的差异 三、镜像和容器 四、Docker Hub 五、Docker架构 六、总结 一、介绍 Docker 是一种开源的应用容器平台&#xff0c;可以在容器内部…...

卷积神经网络(CNN)多种图片分类的实现

文章目录 前期工作1. 设置GPU&#xff08;如果使用的是CPU可以忽略这步&#xff09;我的环境&#xff1a; 2. 导入数据3.归一化4.可视化 二、构建CNN网络模型三、编译模型四、训练模型五、预测六、模型评估 前期工作 1. 设置GPU&#xff08;如果使用的是CPU可以忽略这步&#…...

【备忘录】Docker容器、镜像删除与资源清理命令

文章目录 一&#xff0c;删除容器二&#xff0c;删除镜像三&#xff0c;清理资源 一&#xff0c;删除容器 # 启动时设置 --rm 选项&#xff0c;容器退出时会自动清理内部文件系统 # --rm 不要与 -d 同时使用&#xff0c;或者说同时使用没有意义 docker run --rm #停止所有的容…...

使用 Splashtop 的开放 API 简化 IT 工作流程

我们的工作方式在不断变化&#xff0c;IT 技术人员必须迅速适应时代的变化。越来越多的公司正在转向混合和远程策略&#xff0c;这为那些在服务台或IT技术人员工作的人增加了额外的工作层。对于系统管理员来说&#xff0c;管理一切都可能变得更加复杂。 找到合适的软件来管理多…...

使用requests库进行网络爬虫:IP请求错误的解决方法

目录 引言 一、了解requests库 二、遇到的问题 三、解决方法 1、随机化IP地址 2、减少请求频率 3、使用User Agent模拟浏览器行为 4、使用Cookies 四、注意事项 五、使用代理池 六、总结 引言 在利用Python的requests库进行网络爬虫操作时&#xff0c;我们有时会遇…...

Web之CSS笔记

Web之HTML、CSS、JS 二、CSS&#xff08;Cascading Style Sheets层叠样式表&#xff09;CSS与HTML的结合方式CSS选择器CSS基本属性CSS伪类DIVCSS轮廓CSS边框盒子模型CSS定位 Web之HTML笔记 Web之JavaScript(jQuery)笔记 二、CSS&#xff08;Cascading Style Sheets层叠样式表&…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

Unit 1 深度强化学习简介

Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库&#xff0c;例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体&#xff0c;比如 SnowballFight、Huggy the Do…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

Android 之 kotlin 语言学习笔记三(Kotlin-Java 互操作)

参考官方文档&#xff1a;https://developer.android.google.cn/kotlin/interop?hlzh-cn 一、Java&#xff08;供 Kotlin 使用&#xff09; 1、不得使用硬关键字 不要使用 Kotlin 的任何硬关键字作为方法的名称 或字段。允许使用 Kotlin 的软关键字、修饰符关键字和特殊标识…...

STM32---外部32.768K晶振(LSE)无法起振问题

晶振是否起振主要就检查两个1、晶振与MCU是否兼容&#xff1b;2、晶振的负载电容是否匹配 目录 一、判断晶振与MCU是否兼容 二、判断负载电容是否匹配 1. 晶振负载电容&#xff08;CL&#xff09;与匹配电容&#xff08;CL1、CL2&#xff09;的关系 2. 如何选择 CL1 和 CL…...