mysql---day05

这篇博客主要探讨了MySQL的查询技术,包括联合查询和DML语言的应用,同时还涉及了DDL语言对数据库和表的管理,以及常用的数据类型介绍。通过一系列的大练兵练习,帮助读者巩固和深化MySQL的知识。
摘要由CSDN通过智能技术生成

MySql—查询四

  • 联合查询
  • DML语言(数据操作语言—增删改查)
  • DDL (数据定义–库和表的管理)
  • 常见数据类型

温故知新

######每日复习
#四、SQL语法:.内连接
语法:
select 查询列表
from1 别名
【inner】 join 表2 别名 on 连接条件
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序列表
limit 偏移,显示的数据的条数;

特点:
》表的顺序可以交换
》内连接的结果=多表的交集
》n表连接至少需要n-1个连接条件

分类:
等值连接
非等值连接
 自连接
 
 2.外连接
 select 查询列表
from1 别名
left|right|full【outer 】 join 表2 别名 on 连接条件
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序列表
limit 偏移,显示的数据的条数;

特点:
》查询的结果= 主表中所有的行,如果从表和它匹配的将显示是匹配行,如果从表没有匹配的则显示null
》left join 左边的就是主表,right join右边的就是主表
full join两边都是主表
》一般用于查询除了交集部分的剩余的不匹配的行

3.交叉连接
语法:
select 查询列表
from1 别名
cross join 表2 别名;

特点:
类似于笛卡尔乘积

######################
子查询
一、含义:
嵌套在其他语句内部的select 语句称为子查询或内查询,外面的语句可以是 insert update delete  select 等,一般select作为外面语句较多
外面语句如果为select语句,则此语句称为外查询或主查询
二、分类
1.按出现位置
select后面:仅仅支持标量子查询
from后面:表子查询
where或having后面:标量子查询、列子查询、行子查询
exists后面:标量子查询,列子查询,行子查询,表子查询
2.按结果集的行列
标量子查询(单行子查询):结果集为一行一列
列子查询(多行子查询):结果集为多行一列
行子查询:结果集为多行多列
表子查询:结果集为多行多列

三、示例
where 或having 后面
1.标量子查询
案例:查询最低工资 的员工姓名和工资
     1)最低工资
     select min(salary) from employees
     2)查询员工的姓名和工资,要求工资=1)
     select last_name,salary
     from employees
     where salary=(
     	select min(salary) from employees
     );
     
 2.列子查询
 案例:查询所有是领导的员工姓名
 1)查询所有员工的manager_id
 select manager_id 
 from employees
 2)查询姓名,employee_id属于1)列表的一个
 select last_name
 from employees
 where employee_id in(
 	select manager_id 
     from employees
 );
     
#######################
分页查询
一、应用场景:
当查询的条目数太多,一页显示不全
二、语法:
select 查询列表
from 表
limit [offset,] size;
注意:
offset代表的是起始的条目索引,默认从0卡死
size代表的是显示的条目数

公式:
加入要显示的页数为page,每一页条目数为size
select 查询列表 
from 表
limit [page-1]*size,size;

大练兵1

###经典例题讲解

#1.查询工资最低的员工信息:last_name,salary

#1)查询最低的工资
select min(salary)
from employees

#2)查询last_name,salary,要求salary=1)
select last_name,salary
from employees
where salary=(
    select min(salary)
    from employees
);
#2.查询平均工资最低的部门信息
#方式一:
 #1)各部门的平均工资
 select avg(salary),department_id
 from employees
 group by department_id
 #2)查询1)结果上的最低平均工资
 select min(ag)
 from (
     select avg(salary) as ag,department_id
     from employees
     group by department_id
 )as ag_dep
 
 ##3)查询哪个部门的平均工资=2)
 select avg(salary),department_id
 from employees
 group by department_id
 having avg(salary) = (
      select min(ag)
 from (
     select avg(salary) as ag,department_id
     from employees
     group by department_id
 )as ag_dep
 
 );
 
 #查询部门信息
 select d.*
 from departments as d
 where d.`department_id`=(
     select avg(salary),department_id
 from employees
 group by department_id
 having avg(salary) = (
      select min(ag)
 from (
     select avg(salary) as ag,department_id
     from employees
     group by department_id
 )as ag_dep
 
 )
 );
 
 #方式二:
 #1)各个部门的平均工资
 select avg(salary),department_id
 from employees
 group by departmnent_id
 
 #2)求出最低平均工资的部门编号
 select department_id
 from eployees
 group by department_id
 order by avg(salary)
 limit 1;
 #3)查询部门信息
 select *
 from departments
 where department_id = (
     select department_id
     from eployees
     group by department_id
     order by avg(salary)
     limit 1;
     
 );
#3.查询平均工资最低的部门信息和该部门的平均工资
#1)各个部门的平均工资
 select avg(salary),department_id
 from employees
 group by departmnent_id
 
 #2)求出最低平均工资的部门编号
 select avg(salary),department_id
 from eployees
 group by department_id
 order by avg(salary)
 limit 1;
  #3)查询部门信息
  select d.*,ag
  from departments as d
  join (
              select avg(salary) as ag,department_id
         from eployees
         group by department_id
         order by avg(salary)
         limit 1;
  )as ag_dep
  on d.`department_id` = ag_dep.department_id;
  
#4.查询平均工资最高的job信息
#1)查询最高的job的平均工资
select avg(salary),job_id
from employees
group by job_id
order by avg(salary) desc
limit 1

#2)查询job信息
select *
from jobs
where job_id =(
        select job_id
        from employees
        group by job_id
        order by avg(salary) desc
        limit 1
);

#5.查询平均工资高于公司平均工资的部门有哪些?
#1)查询平均工资
select avg(salary)
from employees

#2)查看每个部门的平均工资
select avg(salary),department_id
from employees
group by department_id

#3)筛选2)的结果集,满足平均工资>1)
select avg(salary),department_id
from employees
group by department_id
having avg(salary)>(
    select avg(salary)
   from employees

);

#6.查询出公司中所有manager的详细信息
#1)查询所有manager的员工编号
select distinct manager_id
from employees

#2)查询详细信息,满足employee_id=1)
select *
from employee_id = any(
    select distinct manager_id
	from employees
);
#7.各个部门中最高工资中最低的哪个部门的最低最低工资是多少
#1)查询各部门的最高工资中最低的部门编号
select department_id
from empoyees
group by department_id
order by max
mysql-connector-j 是 MySQL 官方提供的 Java 连接器,用于在 Java 程序中连接 MySQL 数据库。它的文件结构如下: ``` mysql-connector-java-x.x.xx.jar ├── META-INF │ ├── MANIFEST.MF │ └── maven │ └── mysql │ └── mysql-connector-java │ ├── pom.properties │ └── pom.xml ├── com │ └── mysql │ ├── jdbc │ │ ├── Blob.class │ │ ├── CallableStatement.class │ │ ├── Clob.class │ │ ├── Connection.class │ │ ├── DatabaseMetaData.class │ │ ├── Date.class │ │ ├── Driver.class │ │ ├── DriverManager.class │ │ ├── ParameterMetaData.class │ │ ├── PreparedStatement.class │ │ ├── ResultSet.class │ │ ├── ResultSetMetaData.class │ │ ├── RowId.class │ │ ├── Savepoint.class │ │ ├── SQLClientInfoException.class │ │ ├── SQLException.class │ │ ├── SQLData.class │ │ ├── SQLInput.class │ │ ├── SQLOutput.class │ │ ├── SQLPermission.class │ │ ├── SQLWarning.class │ │ ├── SQLXML.class │ │ ├── Statement.class │ │ ├── Struct.class │ │ ├── Time.class │ │ ├── Timestamp.class │ │ └── Types.class │ ├── jdbc2 │ │ ├── optional │ │ │ ├── Blob.class │ │ │ ├── Clob.class │ │ │ ├── Connection.class │ │ │ ├── DatabaseMetaData.class │ │ │ ├── Date.class │ │ │ ├── ParameterMetaData.class │ │ │ ├── PreparedStatement.class │ │ │ ├── ResultSet.class │ │ │ ├── ResultSetMetaData.class │ │ │ ├── Statement.class │ │ │ ├── Time.class │ │ │ ├── Timestamp.class │ │ │ └── Types.class │ │ ├── optional │ │ │ ├── Blob.class │ │ │ ├── Clob.class │ │ │ ├── Connection.class │ │ │ ├── DatabaseMetaData.class │ │ │ ├── Date.class │ │ │ ├── ParameterMetaData.class │ │ │ ├── PreparedStatement.class │ │ │ ├── ResultSet.class │ │ │ ├── ResultSetMetaData.class │ │ │ ├── Statement.class │ │ │ ├── Time.class │ │ │ ├── Timestamp.class │ │ │ └── Types.class │ │ └── optional │ │ ├── Blob.class │ │ ├── Clob.class │ │ ├── Connection.class │ │ ├── DatabaseMetaData.class │ │ ├── Date.class │ │ ├── ParameterMetaData.class │ │ ├── PreparedStatement.class │ │ ├── ResultSet.class │ │ ├── ResultSetMetaData.class │ │ ├── Statement.class │ │ ├── Time.class │ │ ├── Timestamp.class │ │ └── Types.class │ └── statements │ ├── CallableStatement.class │ ├── PreparedStatement.class │ ├── Statement.class │ └── StatementImpl.class └── java └── time ├── Clock.class ├── Duration.class ├── Instant.class ├── LocalDate.class ├── LocalDateTime.class ├── LocalTime.class ├── Month.class ├── MonthDay.class ├── OffsetDateTime.class ├── OffsetTime.class ├── Period.class ├── Year.class ├── YearMonth.class ├── ZonedDateTime.class └── ZoneId.class ``` 其中,最重要的文件是 `mysql-connector-java-x.x.xx.jar`,这是一个 Java 归档文件,包含了连接 MySQL 所需的所有类和资源。在该文件中,`META-INF` 目录下是元数据信息,`com.mysql.jdbc` 是连接 MySQL 所需的核心类,`java.time` 是 Java 8 中的时间 API,用于与 MySQL 中的日期和时间数据交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值