mysql续

复制代码
--自然连接,隐式内连接
SELECT * FROM    emp,    dept WHERE    emp.DEPTNO = dept.DEPTNO;

--显示内连接
SELECT * FROM    emp INNER JOIN    dept ON    emp.DEPTNO =dept.DEPTNO;

--外连接:左外连接:左边的必须查出来,就算没有on后的条件。。
SELECT * FROM    emp LEFT JOIN    dept    ON    emp.DEPTNO=dept.DEPTNO;

--右外连接:关键字:right 和 left指定主表是那边的,从表是那边的。
SELECT * FROM    emp    RIGHT JOIN    dept    ON    emp.DEPTNO=dept.DEPTNO;

SELECT    @@autocommit;//1标识自动提交,0标识关闭自动提交

--显示开启事物
START TRANSACTION
--提交事物
COMMIT;
--回滚
ROLLBACK;
复制代码

中间表

1.中间表中至少两个字段,这两个字段分别作为外键指向各自一方的主键

2. 为了让两个字段有非空和唯一的约束,通常让这两个字段组成联合主键。

多表的关系

1. 一对一可以看成是特殊的一对多(实体的映射,表的建表结构)

2. 多对多可以看成是两个双向的一对多(实体的映射)

3. 表与表之间的关联操作

  • 插入数据,主表没有限制,从表的外键列的值必须来自主表
  • 修改与删除数据,可以使用级联操作(一定建立在特定的业务逻辑上)
  • 多表的关联查询
复制代码
# 创建部门表
create table dept(
id int primary key auto_increment,
name varchar(20)
);
insert into dept (name) values ('开发部'),('市场部'),('财务部');
# 创建员工表
create table emp (
id int primary key auto_increment,
name varchar(10),
gender char(1), -- 性别
salary double, -- 工资
join_date date, -- 入职日期
dept_id int,
foreign key (dept_id) references dept(id) -- 外键,关联部门表(部门表的主键)
) ;
insert into emp(name,gender,salary,join_date,dept_id) values('孙悟空','',7200,'2013-02-24',1);
insert into emp(name,gender,salary,join_date,dept_id) values('猪八戒','',3600,'2010-12-02',2);
insert into emp(name,gender,salary,join_date,dept_id) values('唐
僧','',9000,'2008-08-08',2);
insert into emp(name,gender,salary,join_date,dept_id) values('白骨精','',5000,'2015-10-07',3);
insert into emp(name,gender,salary,join_date,dept_id) values('蜘蛛精','',4500,'2011-03-14',1);
复制代码

关联查询

1. 当查询的数据来自多张表的时候就需要进行关联查询

2. 当表之间没有主从关系时,使用内连接,重点就是关联条件(外键)

3. 当表之间有主从关系时(全部、所有...),使用外连接,重点1分清主从,2关联条件(外键),推荐使 用左外

复制代码
-- 需求:查询有部门的员工及其所在的部门
-- 需求:查询有员工的部门及其下面的员工
-- 内连接:符合条件的数据会被展示出来
-- 有两种形式:这两种形式在结果上是等效,区别在于显示的左表和右表可以从位置上区分,隐式则不
会作严格的区别
-- 显示内连接 inner是可选的
select * from emp join dept on dept.id = emp.dept_id;
-- 隐式内连接
select * from emp, dept where dept.id = emp.dept_id;
-- 外连接
-- 会严格区分主表和从表,left/right指向的左表或者右表是主表,查询的数据以主表为主
-- 写在left/right outer join左表是左表,右边是右表
-- 需求:查询所有的员工及其所在的部门
select * from emp right outer join dept on dept.id = emp.dept_id;
-- 需求:查询所有的部门及其部门下的员工
select * from dept left join emp on dept.id = emp.dept_id;
-- 使用left指定主表的连接方式叫做左外连接,使用right指定主表的连接方式叫做右外连接
-- 自连接
-- 树形结构的查询
-- 案例:找出每个员工的上级领导,要求显示员工名和对应的领导名。
-- select * from 员工表 left join 领导表 on 员工表.pid = 领导表.id;
select e.name as '员工名',l.name as '领导名' from emp as e left join emp l on
e.pid = l.id where e.pid is not null;
-- 查询出大于平均成绩的学生?
select avg(score),sum(score) from student;
select * from student where score > 86.31;
-- 子查询
-- 子查询的结果要和条件需要的数据形式上一致
select * from student where score > (select avg(score) from student);
-- 需求:查询工资大于 平均工资 的员工,来自于哪些部门的名字
select avg(salary) from emp;
select distinct dept_id from emp where salary > 5000 and dept_id is not
null;
select name from dept where id in(select distinct dept_id from emp where
salary > (select avg(salary) from emp) and dept_id is not null);
select * from emp where join_date >='2011-1-1';
select * from (select * from emp where join_date >='2011-1-1') as e, dept as
d where e.dept_id = d.id;
-- tom选了那些课
select c.name as 课程名称 from student as s, student_course as so, course as
c where s.id = so.s_id and so.c_id = c.id and s.id = 1;
复制代码

 

posted on   白嫖老郭  阅读(139)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示