MySQL高级查询简例

版权声明:本文为博主原创文章,未经允许不得转载 https://blog.csdn.net/qq_38545819/article/details/85933587

多表查询
1,内连接
1.1 等值连接
方式一:=
查询所有员工的员工和部门信息(查询员工和部门的信息)
select * from emp,dept 产生一个笛卡尔集
下面就是避免笛卡尔集的方式:等值条件

select * from emp,dept where emp.deptno = dept.deptno 

方式二:join on

select *from emp join deptonemp.deptno = dept.deptno

方式二:inner join on

select 	*from emp inner join dept on emp.deptno = dept.deptno;

1.2 非等值连接
查询所有员工的姓名和薪资等级信息

select * from emp,salgrade  where sal between salgrade.losal and  salgrade.hisal

1.3 自连接
查询每门课程的先修课名称

select fir.*,sec.cname from course fir,course sec
		where fir.cpno = sec.cno

查询每门课程的间接先修课名称(先修课的先修课)

	select 
		fir.cno,fir.cname,third.cno,third.cname
	from 
		course fir,course sec,course third
	where 
		fir.cpno = sec.cno
	and 
		sec.cpno = third.cno
	改造:
	select 
		fir.cno,fir.cname,third.cno,third.cname
	from 
		course fir
	inner join 
		course sec
	on
		fir.cpno = sec.cno
	inner join
		course third
	on
		sec.cpno = third.cno	
	


	总结:内连接的特点 :求交集
	select * from course	
	select fir.*,sec.cname from course fir,course sec
	where fir.cpno = sec.cno	

2,外连接
左外连接
查询所有员工的部门信息

select 	* from emp left join dept	on	emp.deptno = dept.deptno

右外连接

select 	* from 	emp 	right join 	dept on	emp.deptno = dept.deptno;

select * from emp,dept where emp.deptno(+) = dept.deptno;(Oracle中可以如此,)

3,外键
3.1 什么是外键

本质就是一种约束。

3.2 外键的四个约束

3.3 外键的条件

3.4 建立外键
3.4.1 建表的同时建外键
create table emp(

foreign key(外键字段) references dept(deptno)
)

3.4.2 建立表之后添加外键
alter table emp add foreign key(deptno) references dept(deptno)

3.4.3删除外键:
语法:alter table 表名称 drop foreign key 外键名称;
例:alter table empA drop foreign key empa_ibQk_1;
注意:如果没有在建表的时候标明外键名称,可以通过:
show create table 表名 进⾏查看外键名称;

4,子查询
4.1 标量子查询(子查询的结果是1行1列)
查询与egz同部门的员工信息
select * from emp where deptno = egz的部门号相等

select * from emp where deptno =(select deptno from emp where ename='egz')

4.2 列子查询(子查询的结果是N行1列)
查询与部门10工作岗位相同的员工信息

select * from emp where empjob in (select empjob from emp where deptno = 10)

4.3 行子查询(1行N列)
查询与egz部门和岗位都相同的员工信息

select * from emp where (deptno,empjob) = (select deptno,empjob from emp where ename='egz')

4.4 表子查询(N行N列)
查询与10号部门相同的岗位和上司的员工信息

select * from emp where (empjob,mgr) in (select empjob, mgr from emp where deptno = 10)

5,exists 存在

select * from emp where deptno =10 and exists
	(select * from dept where emp.deptno = dept.deptno and emp.deptno =20)

6, 派生表:子查询查询出来的表,叫虚表 放到from中就叫派生表

select emp.ename,emp.empjob,a.deptno,a.dname
	from emp,(select deptno,dname from  dept where deptno>10) a
	where emp.deptno = a.deptno

7,union 与 union all
select * from emp
union
select * from emp

select * from emp 
union all
select * from  emp


select dept.* ,count(dept)from dept, emp;

select * from emp,dept where emp.deptno = dept.deptno;

select dept.deptno, dept.dname,dept.loc,count(empno) from emp ,dept where emp.deptno = dept.deptno group by deptno;

实例的表:

create table DEPT
(
  DEPTNO int(2) not null primary key,
  DNAME  VARCHAR(14),
  LOC    VARCHAR(13)
)

create table EMP
(
  EMPNO    int(4) not null,
  ENAME    VARCHAR(10),
  EMPJOB   VARCHAR(9),
  MGR      int(4),
  HIREDATE DATE,
  SAL      decimal(7,2),
  COMM     decimal(7,2),
  DEPTNO   int(2)
)


create table SALGRADE
(
  GRADE int not null primary key,
  LOSAL decimal(7,2),
  HISAL decimal(7,2)
)

猜你喜欢

转载自blog.csdn.net/qq_38545819/article/details/85933587