MySQL-Day3

学习目标

写SQL三步法

边写边运行,否则后面出错时候会难以排查

搭框架

基本的select语句框架建起来,如果有多表,把相应的多表联合起来

看条件

决定where后面的

显示的字段

select后面的内容

连接查询

内连接

两张表相同地方

select * from 

左/右连接

包括内连接以及左/右部分特有的内容

隐式内链接(不怎么用)

表的别名(主要用在查询方便)

带有where的内连接

select * from 表1 inner join 表2 on 表1.字段=表2.字段 where 条件

多表内连接查询

连接三张表,students,scores,courses

select * from students st inner join scores sc on ststudentNo=scstudentNo inner join cources co 

on sc.coursesNo=co.courseNo;

练习--where

  • 查询所有学生的'linux'课程成绩,要求只显示姓名、成绩、课程名

分三步写

select name,score,coursename from students st inner join scores sc

on st.studentNo=sc.studentNo

inner join courses co on sc.courseNo=co.courseNo

where courseName='linux'

显示字段中,如果是表中唯一的字段,可以不用带表名,反之需要带

练习--order by

  • 查询成绩最高的男生信息,要求只显示姓名、课程名、成绩

select name,courseName,score from students st inner join scores sc

on st.studentNo=sc.studentNo

inner join courses co on sc.courseNo=co. courseNo

where sex='男'

order by score  desc limit 1;//显示第一行

左/右连接

  • 查询所有学生的信息以及成绩,包括没有成绩的学生

select * from students st left join scores sc

on st.studentsNo=sc.studentsNo;

  • 查询所有课程信息,包括没有成绩的课程

select * from scores sc right join courses co

on co.courseNo=sc.courseNo;

自关联

将多张表合成一张表

同一张表做连接查询;一定是一张表可关联不同字段

子查询

子查询嵌入到主查询中;

主查询不能独立运行

  • 查询大于平均年龄的学生记录

select * from students where age>(select avg(age) from students )

标量子查询

返回值是1*1的数据

列子查询--返回一列多行

查询30岁的学生的成绩

用子查询比内连接好点

select * from scores where students in (select studentNo from students where age=30 );

表级子查询--多行多列

用子查询,查询所有女生的信息和成绩

select * from (select * from students where sex='女') stu---当做一个表

inner join scores sc on stu.studentNo=sc.studentNo;

习题

  • 男职工的总数和女职工的总数

select sex,count(*) from employees group by sex;

  • 非党员职工的总数

select count(*) from employees where politicalstatus<>'党员';

  • 所有职工工号,姓名以及所在部门名称

select empid,empname,deptname from employees e inner join deparments d on e.deptid=d.deptid;

  • 所有职工工号,姓名和对应工资

select empid,empname,salary from employees e inner join salary s on e.empid=s.empid;

  • 职工总人数>4的部门号和部门名称

select e.deptid,deptname from employees e inner join departments d

on e.deptid=d.deptid  group by e.deptid having count(*)>4;

  • 列出开发部和测试部的职工号,姓名

select empid,empname from employees e inner join departments d on e.deptid=d.deptid where deptname in ('开发部','测试部')

  • 列出市场部所有女职员的姓名和政治面貌

select empname,politicalstatus from empoyees e inner join departments d on 

e.deptid=d.deptid

where e.sex='女' i and deptname='市场部'  

  • 显示所有职工姓名和工资,包括没有工资的职工姓名

select empname,salary from employees e left join salary s on e.emptid=s.emptid

求不姓孙 的所有员工工资总和

select sum(salary) from employees e inner join salary s on

 e.emptid=s.emptid where not empname like '孙%'

总结

  • 33
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值