数据库---3 单表 多表的查询

单表查询

前期表准备

create table emp(
  id int not null unique auto_increment,
  name varchar(20) not null,
  sex enum('male','female') not null default 'male', #大部分是男的
  age int(3) unsigned not null default 28,
  hire_date date not null,
  post varchar(50),
  post_comment varchar(100),
  salary double(15,2),
  office int, #一个部门一个屋子
  depart_id int
);

#插入记录
#三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tank','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('nick','male',18,'19000301','teacher',30000,401,1),
('sean','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;

#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk

1.语法执行顺序

# 初识查询语句
select id,name from emp where id >= 3 and id <= 6;
# 先后顺序
from
where
select

2.where约束条件

# 1.查询id大于等于3小于等于6的数据
select id,name from emp where id >= 3 and id <= 6;
select *  from emp where id between 3 and 6;  

# 2.查询薪资是20000或者18000或者17000的数据

select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,18000,17000);  # 简写

# 3.查询员工姓名中包含o字母的员工姓名和薪资
# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句
"""
先是查哪张表 from emp
再是根据什么条件去查 where name like ‘%o%’
再是对查询出来的数据筛选展示部分 select name,salary
"""
select name,salary from emp where name like '%o%';

# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;

# 5.查询id小于3或者大于6的数据
select *  from emp where id not between 3 and 6;

# 6.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);

# 7.查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is
select name,post from emp where post_comment = NULL;  # 查询为空!
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;

3.group by

# 数据分组应用场景:每个部门的平均薪资,男女比例等

# 1.按部门分组
select * from emp group by post;  # 分组后取出的是每个组的第一条数据
select id,name,sex from emp group by post;  # 验证
"""
设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据,
不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取
"""
set global sql_mode="strict_trans_tables,only_full_group_by";
# 重新链接客户端
select * from emp group by post;  # 报错
select id,name,sex from emp group by post;  # 报错
select post from emp group by post;  # 获取部门信息
# 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名

# 2.获取每个部门的最高工资
# 以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
# 每个部门的最高工资
select post,max(salary) from emp group by post;
# 每个部门的最低工资
select post,min(salary) from emp group by post;
# 每个部门的平均工资
select post,avg(salary) from emp group by post;
# 每个部门的工资总和
select post,sum(salary) from emp group by post;
# 每个部门的人数
select post,count(id) from emp group by post;

# 3.查询分组之后的部门名称和每个部门下所有的学生姓名
# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
select post,group_concat(name) from emp group by post;

select post,group_concat(name,"_SB") from emp group by post;

select post,group_concat(name,": ",salary) from emp group by post;

select post,group_concat(salary) from emp group by post;

# 4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用
select name as 姓名,salary as 薪资 from emp;
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;

# 补充as语法 即可以给字段起别名也可以给表起
select emp.id,emp.name from emp as t1; # 报错  因为表名已经被你改成了t1
select t1.id,t1.name from emp as t1;

# 查询四则运算
# 查询每个人的年薪
select name,salary*12 as annual_salary from emp;
select name,salary*12 annual_salary from emp;  # as可以省略

练习题

# 刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表也没有限制条件,再确定是否需要分类,最后再确定需要什么字段对应的信息
# 相对于上面的表
1. 查询岗位名以及岗位包含的所有员工名字
select post,group_concat(name) from emp group by post
2. 查询岗位名以及各岗位内包含的员工个数
select post,count(id)from emp group by post;
3. 查询公司内男员工和女员工的个数
select sex,count(id) from emp group by sex;
4. 查询岗位名以及各岗位的平均薪资
select post,avg(salary) from emp group by post;
5. 查询岗位名以及各岗位的最高薪资
select post,max(salary) from emp group by post;
6. 查询岗位名以及各岗位的最低薪资
select post,min(salary) from emp group by post;
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
select sex,avg(salary) from emp group by sex;

"""
参考答案:此处无意义  我没有建表.......
select post,group_concat(name) from emp group by post;
select post,count(id) from emp group by post;
select sex,count(id) from employee group by sex;
select post,avg(salary) from emp group by post;
select post,max(salary) from employee group by post;
select post,min(salary) from employee group by post;
select sex,avg(salary) from employee group by sex;
"""

# 关键字where group by同时出现的情况下,group by必须在where之后
# where先对整张表进行一次筛选,如何group by再对筛选过后的表进行分组
# 如何验证where是在group by之前执行而不是之后 利用聚合函数 因为聚合函数只能在分组之后才能使用
select id,name,age from emp where max(salary) > 3000;  # 报错!

select max(salary) from emp;
# 正常运行,不分组意味着每一个人都是一组,等运行到max(salary)的时候已经经过where,group by操作了,只不过我们都没有写这些条件

# 语法顺序
select
from
where
group by

# 再识执行顺序
from
where
group by
select

8、统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) from emp where age > 30 group by post;
# 对where过滤出来的虚拟表进行一个分组

# 还不明白可以分步执行查看结构
select * from emp where age>30;
# 基于上面的虚拟表进行分组
select * from emp where age>=30 group by post;

4.having

截止目前已经学习的语法

select 查询字段1,查询字段2,... from 表名
        where 过滤条件
        group by分组依据

# 语法这么写,但是执行顺序却不一样
from
where
group by
select

having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!

1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
        where age >= 30
        group by post
        having avg(salary) > 10000;
# 如果不信你可以将having取掉,查看结果,对比即可验证having用法!

#强调:having必须在group by后面使用
select * from emp having avg(salary) > 10000;  # 报错

5.distinct

# 对有重复的展示数据进行去重操作
select distinct post from emp;

6.order by

select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排

select * from emp order by age desc; #降序排

#先按照age降序排,在年轻相同的情况下再按照薪资升序排
select * from emp order by age desc,salary asc; 

# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
select post,avg(salary) from emp
    where age > 10
    group by post
    having avg(salary) > 1000
    order by avg(salary)
    ;

7.limit

# 限制展示条数
select * from emp limit 3;
# 查询工资最高的人的详细信息
select * from emp order by salary desc limit 1;

# 分页显示
select * from emp limit 0,5;  # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置
select * from emp limit 5,5;

8.正则

select * from emp where name regexp '^j.*(n|y)$';

多表查询

表创建

#建表
create table dep(
id int,
name varchar(20)
);

create table emp(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);

#插入数据
insert into dep values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');

insert into emp(name,sex,age,dep_id) values
('jason','male',18,200),
('egon','female',48,201),
('kevin','male',38,201),
('nick','female',28,202),
('owen','male',18,200),
('jerry','female',18,204)
;

# 当初为什么我们要分表,就是为了方便管理,在硬盘上确实是多张表,但是到了内存中我们应该把他们再拼成一张表进行查询才合理

表查询

select * from emp,dep;  # 左表一条记录与右表所有记录都对应一遍>>>笛卡尔积

# 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据

# 查询员工及所在部门的信息
select * from emp,dep where emp.dep_id = dep.id;
# 查询部门为技术部的员工及部门信息
select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';

# 将两张表关联到一起的操作,有专门对应的方法
# 1、内连接:只取两张表有对应关系的记录
select * from emp inner join dep on emp.dep_id = dep.id;
select * from emp inner join dep on emp.dep_id = dep.id
                            where dep.name = "技术";

# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp left join dep on emp.dep_id = dep.id;

# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp right join dep on emp.dep_id = dep.id;

# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;

子查询

# 就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用
# 1.查询部门是技术或者人力资源的员工信息
"""
先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息
"""
select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");

# 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询
select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date
;

"""
记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询
"""

select * from emp inner join dep on emp.dep_id = dep.id;
# 1.查询id大于等于3小于等于6的数据
select id from emp where id >= 3 and id <= 6;
select id from emp where id between 3 and 6;
# 2.查询薪资是20000或者18000或者17000的数据
select * from emp where salary = 20000 or salary = 17000 or salary = 18000;
select id,name,salary from emp where salary = 20000 or salary = 17000 or salary = 18000;
# 3.查询员工姓名中包含o字母的员工姓名和薪资
select name,salary from emp where name like '%o%';
# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
# 5.查询id小于3或者大于6的数据
select id from emp where id < 3 or id > 6;
select id from emp where id < 3 and id > 6;
# 6.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (17000,18000,20000);
select * from emp where salary != 17000 and salary !=18000 and salary != 20000;
# 7.查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is
select name, post from emp where post_comment is null;

# 1.按部门分组
select * from emp group by;
# 2.查询薪资是20000或者18000或者17000的数据
# 3.查询员工姓名中包含o字母的员工姓名和薪资
# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
# 5.查询id小于3或者大于6的数据
# 6.查询薪资不在20000,18000,17000范围的数据
# 7.查询岗位描述为空的员工名与岗位名

原文地址:https://www.cnblogs.com/xuzhaolong/p/11391265.html

时间: 2024-08-30 06:21:32

数据库---3 单表 多表的查询的相关文章

[数据库] Oracle单表查询总数及百分比和数据横向纵向连接

这是最近项目关于SQL语句的,本文简单记录并总结以下几个知识点: 1.如何统计一张表中某个字段的总数,如不同"专业"的学生数及所占百分比: 2.如何联系另一张表进行查询某个字段的总数及百分比: 3.简单介绍decode防止分母为0和trunc保留小数位数等函数: 4.通常复杂的SQL语句会涉及到查询结果横向连接和纵向连接,这里进行介绍. 最近买了本<Oracle查询优化改写技巧与案例·有教无类 落落>,推荐大家也阅读下.后面我也会补充一些相关数据的知识,希望对大家有所帮助吧

oracle数据库之单表查询

作为一合格的测试人员对数据库的单表查询.多表查询.分组查询.子查询等等这些基本查询方法还是要会的.不然到企业中,容易被一些人鄙视,或者说如果数据库学不好,表查不明白,那么对自己能力来说也是一种侮辱,因为你可以证明自己,你是可以的,尤其是你在面试的时候面对面试官的给你出的一道sql语句题目,你能马上用你的套路把这道题做出了,那么恭喜你过了第一个小关卡.ok,我们今天学习一下数据库中的第一个查询,也是最简单,容易入门的查看----单表查询. 我们在上一篇中创建了一个classinfo和student

基于MySql数据库的单表与多表联合查询

这里以学生 班级 身份证 以及课程为例 1,启动MySql数据库  开启服务 2.1.0新建一张班级表 备注:CHARSET = UTF8 (指定编码格式为utf8 防止中文乱码) /*班级表*/ CREATE TABLE CLASS_INFO( C_ID INT PRIMARY KEY, CLASS_NAME VARCHAR(20) not NULL )CHARSET = UTF8; 运行效果: 新建成功 2.1.1依次建好学生表(学生表有一个指向班级表的主键  以便做关联查询) /*学生表*

数据库9:联结表 高级联结 组合查询 全文本搜索

第十五章联结表 Sql最强大的功能之一就是能在数据检索查询的执行中联结(join)表.联结是利用sql的select能执行的最重要的操作,能很好的理解联结及其语法是学习sql的一个极为重要的组成部分.   外键:外键为某个表中的一列,它包含另一个表的主键值,定义了两个表之间的关系. 好处:供应商信息不重复,不浪费空间和时间,方便日后修改,一个表信息改动不影响另一个表的信息 联结是一种机制,使用特殊的语法,可以联结多个表返回一组输出,联结在运行时关联表中正确的行.   创建联结          

数据库、数据表的基本操作及查询数据

数据库的基本操作 创建数据库 CREATE DATABASE database_name database_name为要创建的数据库的名称 删除数据库 DROP DATABASE database_name database_name为要删除的数据库的名称 数据库存储引擎 数据库存储引擎是数据库底层软件组成,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据操作. MySQL的核心就是存储引擎. 存储引擎比较 |功能|MyISAM|Memory|InnoDB|Archive|

MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引

注:本文转自:http://www.cnblogs.com/smyhvae/p/4042303.html 本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, 外键可以有重复的, 可以是空值,用来和其他表建立联系用的.所以说,如果谈到了外键,一定是至少涉及到两张表.例如下面这两张表: 上面有两

表单 对数据库字段自动加密解密表单提交(THINKPHP3.2)

1.config配置变量 'MODEL_FIELD_FLAG' => TRUE,//表单加密开关 'MODEL_FIELD_NAME_PRE' => 'mlm_',//表单加密前缀 'MODEL_FIELD_EMCODE' => 'md5',//加密方式 'MODEL_FIELD_EMCODE_KEY' => '[email protected]'//加密key 2.控制器层 $model  继承基础类 BaseModel BaseRelationModel 任意一个的自定义模型

在db2中 两个数据库之间的两个表的联合查询

大家好,今天遇到了在db2中 两个数据库之间的两个表的联合查询我知道oracle中有dblink,可是不知到db2的两个数据库联合查询怎么处理我找了类似于比如两个数据库: db1,db2用户名密码select * from db1.用户名.密码,db2.用户名.密码 where db1.NM=db2.NM可是这样不好用啊请各位帮忙谢谢 DB2有联邦数据库的,你可以查一下. 1.要看目录数据库请用:db2 list db directory这些信息应该是放系统表中.(既不是什么注册表.也不是什么文

Oracle 数据库(oracle Database)Select 多表关联查询方式

Oracle数据库中Select语句语法及介绍 SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名2>[,…] [WHERE <筛选择条件表达式>] [GROUP BY <分组表达式> [HAVING<分组条件表达式>]] [ORDER BY <字段>[ASC | DESC]] 语句说明: []方括号为可选项 [GROUP BY <分组表达式&g

sql语句查询服务器的数据库,数据库的全部表和表的全部列

下面是数据库的结构: 数据库名是:edushi_zixunok;表名是infoArticle --获取所有用户名 SELECT * FROM sys.sysusers --获取所有用户数据库 SELECT * FROM sys.sysdatabases --获取所有的表名277576027 SELECT * FROM edushi_zixunok .sys.tables --获取某个表的字段名1365579903 select * from edushi_zixunok.sys.columns