热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mysql数据库多表查询示例_【数据库】MySQL——多表查询详细介绍以及实例

1.表与表之间的关系一对一:用户表和身份信息表,用户表是主表例如:男人表、女人表createtableman(midintprimaryk

1.表与表之间的关系

一对一:用户表和身份信息表,用户表是主表

例如:男人表 、女人表

create table man(

mid int primary key auto_increment,

mname varchar(32),

wid int unique

);

create table woman(

wid int primary key auto_increment,

wname varchar(32)

);

一对多:最常见的表关系,用户表和订单表

例如:员工表、部门表

create table emp(

empno int primary key auto_increment,

ename varchar(32),

deptno int

);

create table dept(

deptno int primary key auto_increment,

dname varchar(32)

);

多对多:例如学生表和课程表,通常情况都是将多对多的关系拆分为一对多或者多对一的关系。

create table student( sid int primary key auto_increment,

sname varchar(32)

);

insert into student (sname) values ('大拿');

insert into student (sname) values ('唐嫣');

insert into student (sname) values ('王健林');

create table course(

cid int primary key auto_increment,

cname varchar(32)

);

insert into course (cname) values ('语文');

insert into course (cname) values ('数学');

insert into course (cname) values ('英语');

insert into course (cname) values ('化学');

create table s_c(

cid int, sid int

);

insert into s_c (sid,cid) values (1,1);

insert into s_c (sid,cid) values (1,2);

insert into s_c (sid,cid) values (1,3);

insert into s_c (sid,cid) values (1,4);

insert into s_c (sid,cid) values (2,2);

insert into s_c (sid,cid) values (2,4);

insert into s_c (sid,cid) values (3,1);

insert into s_c (sid,cid) values (3,3);

2.为什么要使用多张表

避免出现大量的数据的冗余。

并不是表拆的越多就越好,根据实际情况进行拆分。

3.概念

同时查询多张表

4.分类

<1>合并查询

union &#xff0c;union all

合并结果集&#xff0c;就是把两个select语句的查询结果合并到一起。(相当于并集)

合并的两个结果&#xff0c;列数和列的顺序&#xff0c;类需要一致

create table emp(

empno int primary key auto_increment,

ename varchar(32)

);

create table dept(

deptno int primary key auto_increment,

dname varchar(32)

);

select * from emp union select * from dept;

select * from emp union all select * from dept;

<2>连接查询

员工表

create table emp(

empno int primary key auto_increment, # 员工编号

ename varchar(32), #员工姓名

job varchar(32), #员工职位

mgr int, #上级编号

hiredate date, #入职时间

sal double, #薪水

comm double, #奖金

deptno int #所属部门

);

部门表

create table dept(

deptno int primary key auto_increment, #部门编号

dname varchar(32), #部门名称

loc varchar(32) #部门地址

);

内连接: inner join….on 、 join 、 ,

inner join 是比较运算符&#xff0c;只返回符合条件的行

例如&#xff1a;

select * from emp inner join dept on emp.deptno&#61;dept.deptno;

select * from emp e ,dept d where e.deptno &#61; d.deptno;

select * from emp e join dept d where e.deptno &#61; d.deptno;

外连接:

左外连接&#xff1a;LEFT OUTER JOIN | left join … on

代表查询&#xff0c;左边行的全部&#xff0c;右边没有则null

select * from emp e LEFT OUTER JOIN dept d ON e.deptno &#61; d.deptno;

右外连接&#xff1a; right join … on 或者 right outer join …. on

右连接包含right join 右表所有的行&#xff0c;如果左表中某行在右表没有匹配&#xff0c;则结果中对应的左表的部门全部为空(null)

select * from emp e right OUTER JOIN dept d ON e.deptno &#61; d.deptno;

自连接&#xff1a;

自连接就是说&#xff0c;在同一个数据表中&#xff0c;看作是两个表&#xff0c;表示查找每个人的领导&#xff0c;如果没有领导&#xff0c;则显示无领导

把一张表看作成两张表&#xff0c;一张员工表&#xff0c;一张领导表&#xff0c;都是emp表

select e.ename,el.ename from emp e left join emp el on e.mgr &#61; el.empno;

自然连接:natural join (join)| natural left join(同 left join) | natural right join (同 right join)

自然连接会自动判断&#xff0c;以两个表中相同的字段为连接条件&#xff0c;返回查询结果。

select * from emp natural join dept;

select * from emp NATURAL left join dept;

select * from emp NATURAL right join dept;

注意&#xff1a;内连接不写连接条件会出现笛卡尔积的结果&#xff0c;应该避免这种情况&#xff0c;而外连接不写连接条件会报错。

<3>子查询(ANY子查询、IN子查询、SOME子查询、ALL子查询)

子查询解决的问题&#xff1a;

谁的薪资比丛浩高&#xff1f;&#xff1f;&#xff1f;

select * from emp where sal >(select sal from emp where ename&#61;&#39;从浩&#39;);

定义&#xff1a;子查询允许把一个查询嵌套在另一个查询当中

子查询又叫做内部查询&#xff0c;相当于内部查询。包含内部查询的就称为外部查询。子查询的结果被主查询所使用。

注意的问题&#xff1a;

1.括号

2.可以在主查询的where select having from 后面&#xff0c;都可以使用子查询

3.不可以再group by 后面使用子查询

4.主查询和子查询可以不是同一张表&#xff1b;只有子查询返回的值&#xff0c;主查询可以使用。

需求:查询部门名称是人力的员工信息

第一种方式&#xff1a;利用子查询

select * from emp where deptno&#61;(select deptno from dept where dname&#61;&#39;人力部&#39;);

第二种方式&#xff1a;利用关联查询

select * from emp e,dept d where e.deptno &#61; d.deptno and d.dname&#61;&#39;人力部&#39;;

SQL优化&#xff1a;尽量使用多表查询

绝大部分的子查询再最终执行的时候他都是转换成一个多表查询来执行的。 通过SQL执行计划可以看出来。

通过SQL执行计划会发现两种方式执行的是一样的。

5.from后面的子查询

需求&#xff1a;

查询员工号 姓名 月薪

select empno,ename,sal from emp;

6.一般不在子查询中排序

7.一般先执行子查询&#xff0c;再去执行主查询

ANY关键字

假设any内部的查询返回结果个数是三个&#xff0c;如&#xff1a;result1&#xff0c;result2&#xff0c;result3&#xff0c;那么

select .... from .. where a > any(...);

->select ..... from ... where a > result1 or a >result2 or a >result3;

需求&#xff1a;

查询工资比1号部门中任意一个员工高的信息

select * from emp where sal > any(select sal from emp where deptno &#61; 1);

ALL关键字

ALL关键字与any关键字类似&#xff0c;只不过上面的or改成and &#xff1a;

select .... from .. where a > all(...);

->select ..... from ... where a > result1 and a >result2 and a >result3;

需求:

查询工资比1号部门中所有员工号的员工信息

select * from emp where sal > all(select sal from emp where deptno &#61; 1);

SOME关键字

some 关键字和any关键字是一样的功能。所以&#xff1a;

select .... from .. where a > any(...);

->select ..... from ... where a > result1 or a >result2 or a >result3;

IN关键字

IN运算符用于where表达式中&#xff0c;以列表向的形式支持多个选择。语法如下&#xff1a;

where column in (v1,v2,v3,.....);

where column not in (v1,v2,v3,.....);

当in前面加上not运算符时候&#xff0c;表示与in相反的意思&#xff0c;既不在这写列表项中选择。

案例&#xff1a;

查询部门名称是人力和研发的员工

select * from emp where deptno in (select deptno from dept where dname&#61;&#39;人力部&#39; or dname&#61;&#39;研发部&#39;)

分类: MySQL数据库

想了解更多相关问题请访问PHP中文网&#xff1a;MySQL视频教程



推荐阅读
author-avatar
youyiyang
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有