当前位置:首页 > 编程笔记 > 正文
已解决

查询数据库DQL

来自网友在路上 169869提问 提问时间:2023-11-20 18:00:53阅读次数: 69

最佳答案 问答题库698位专家为你答疑解惑

 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);

 

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"查询数据库DQL":http://eshow365.cn/6-40550-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!