SQL Server【三】连接查询

将两个表或者两个以上的表以一定的连接条件连接起来,从中检索出满足条件的数据。

内连接

使用inner joininner可以省略

-- 查询员工的姓名和部门名称
select "E".ename as "员工姓名", "D".dname as "部门名称"from emp "E"join dept "D"on "E".deptno = "D".deptno

select … from A, B

假设A表有xxx行,则行可以表示为集合(a1,a2,...,ax)(a_1,a_2,...,a_x)(a1,a2,...,ax)
假设B表有yyy行,则行可以表示为集合(b1,b2,...,by)(b_1,b_2,...,b_y)(b1,b2,...,by)
select ... from A,B就是将两个表的行进行笛卡尔成绩,并将两个行进行合并得到(a1+b1,a1+b2,...,a2+b1,a2+b2,...)(a_1+b_1,a_1+b_2,...,a_2+b_1,a_2+b_2,...)(a1+b1,a1+b2,...,a2+b1,a2+b2,...),因此总共的行数是x∗yx*yxy,总共的列数是两个表列数相加

即把A表的每一条记录都和B表的每一条记录组合在一起

select … from A,B where …

对上面的表用where的条件进行过滤

select E.ename as "员工姓名", D.dname as "部门名称"from emp as "E", dept as "D"where E.deptno = D.deptno

select … from A join B on …

join是连接的意思 on 表示连接条件
如果使用join就必须使用on

select * from empjoin depton 1=1	--70*11select emp.ename as '员工姓名', dept.deptno as '部门编号'from empjoin depton 1 = 1 --70*2select emp.ename as '员工姓名', dept.deptno as '部门编号'from empjoin depton emp.deptno=dept.deptno --14*2select E.ename as '员工姓名', D.deptno as '部门编号'from emp as "E"join dept as "D"on E.deptno=D.deptno --14*2
select *from emp as "E"join dept as "D"on 1 =1 select *from dept as "D"join emp as "E"on 1 =1 order by D.deptnoselect * from dept,emp
where dept.deptno = emp.deptno
--实际中发现无论将哪个表放在前面,总是用行数少的表匹配行数多的

fromjoin后面可以使用别名,如果在这里使用别名,其他的地方也都必须使用别名。区别于select后面的别名不能在其他地方使用,我认为根本原因在于语句的执行顺序

实际上和select ... from A,B where ...等价,推荐使用join on

使用join on可以再使用where对得到的数据过滤,从而实现不同的分工

混合使用

select * from emp as "E", dept as "D"
where E.deptno=D.deptno and E.sal>2000
--等价于下面的写法,下面的写法更加清晰
select * from emp as "E"join dept as "D"on E.deptno = D.deptnowhere E.sal > 2000--求出工资大于2000的员工的姓名 部门编号 薪水 薪水等级
select emp.ename as "员工姓名", dept.dname as "部门名称", emp.sal as "薪水", SALGRADE.GRADE as "薪水等级"from emp,dept,SALGRADEwhere emp.deptno=dept.deptno and emp.sal>2000 and emp.sal >= SALGRADE.LOSAL and emp.sal <=SALGRADE.HISALselect  emp.ename as "员工姓名", dept.dname as "部门名称", emp.sal as "薪水", SALGRADE.GRADE as "薪水等级"from empjoin depton emp.deptno=dept.deptnojoin SALGRADEon emp.sal>=SALGRADE.LOSAL and emp.sal<=SALGRADE.HISALwhere emp.sal>2000

我们也可以把查询的表当作一个表,进行子查询

-- 输出部门名称,该部门所有员工的平均工资 平均工资等级select dept.dname as "部门名称", tmp.avg_sal as "平均工资", SALGRADE.GRADE as "平均工资等级"from(select emp.deptno as "dept_no", AVG(emp.sal) as "avg_sal"from empgroup by emp.deptno) "tmp"join depton dept.deptno = tmp.dept_nojoin SALGRADEon tmp.avg_sal between SALGRADE.LOSAL and SALGRADE.HISAL

语句顺序

SELECT ...INTOFROMJOINONWHEREGROUP BYHAVINGORDER BY
-- 输出3个姓名中不含有O的工资最高的员工的姓名、工资、工资等级、部门名称select top 3 emp.ename as "员工姓名", emp.sal as "员工工资", SALGRADE.GRADE as "工资等级", dept.dname as "部门名称"from empjoin depton emp.deptno=dept.deptnojoin SALGRADEon emp.sal >= SALGRADE.LOSAL and emp.sal <= SALGRADE.HISALwhere emp.ename not like '%O%'order by emp.sal desc

nullnot in在一起的时候需要注意。如果表中有null,则使用not in的时候返回的总为空。

这与SQL的比较机制有关。在SQL中比较结果分为true``false``null,只有结果为true的时候系统才认为匹配成功并返回记录,in的本质是等于的ornot in的本质是不等于的and

比较结果and nullor null
truenulltrue
falsefalsenull
nullnullnull

当使用in的时候因为是or进行连接,所以可以正常返回true,在not in的时候是and连接,因此返回总为null,因此返回为空。

详细原因可以看这篇文章:传送门。为了解决这个问题我们可以使用is [not] nullisnull()函数组合判断

--求出emp表中所有领导的姓名select distinct E1.ename as "领导姓名"from emp "E1"join emp "E2"on  E1.EMPNO = E2.mgrselect emp.ename as "领导姓名"from empwhere emp.EMPNO in (select distinct mgr from emp)--输出所有非领导的信息select *from empwhere emp.EMPNO not in (select distinct mgr from emp where mgr is not null)
--求出平均薪水最高的部门的名称和部门平均工资select dept.dname as "部门名称", tmp.avg_sal as "平均工资"from (select top 1 emp.deptno as "dept_no", AVG(emp.sal) as avg_salfrom empgroup by emp.deptnoorder by AVG(emp.sal) desc) "tmp"join depton tmp.dept_no=dept.deptno

当子查询的值只有一个的时候可以将子查询放在表达式中

--工资大于 所有员工中工资最低的人中的工资 的人中
--前三个人的姓名 工资 部门编号 部门名称 工资等级
select top 3 emp.ename as "姓名", emp.sal as "工资", emp.deptno as "部门编号", dept.dname as "部门名称", SALGRADE.GRADE as "工资等级"from empjoin (select MIN(sal) as "min_sal" from emp) as "tmp"on emp.sal > tmp.min_saljoin dept on emp.deptno = dept.deptnojoin SALGRADEon emp.sal between SALGRADE.LOSAL and SALGRADE.HISALorder by emp.sal select top 3 tmp.ename as "姓名", tmp.sal as "工资", tmp.deptno as "部门编号", dept.dname as "部门名称", SALGRADE.GRADE as "工资等级"from ( select ename,sal,deptno from emp where sal > (select MIN(sal) as "min_sal" from emp)) as "tmp"join dept  on tmp.deptno = dept.deptnojoin SALGRADEon tmp.sal between SALGRADE.LOSAL and SALGRADE.HISALorder by tmp.sal
--把工资大于1500的所有员工按部门分组,
--按升序输出最后两个平均工资小于3000的部门名称,人数,平均工资,平均工资水平
select dept.dname as "部门名称", tmp.number as "部门人数", tmp.avg_sal as "平均工资", SALGRADE.GRADE as "平均给工资水平"from(select top 2 deptno as "dept_no", COUNT(*) as "number", AVG(sal) as "avg_sal"from emp where sal>1500 group by deptnohaving AVG(sal)<3000order by AVG(sal) desc) "tmp"join depton tmp.dept_no=dept.deptnojoin SALGRADEon tmp.avg_sal between SALGRADE.LOSAL and SALGRADE.HISALorder by tmp.avg_sal

order by的顺序应该在最后,因此可以用别名。group byhaving都不可以用别名

外连接

不但返回满足条件的所有记录,而且会返回部门不满足条件的记录。

左外连接

select * from empleft join depton emp.deptno=dept.deptno
  • 用左表的一行分别和右表的所有行进行连接,如果没有匹配的行,则一起输出,如果右表有多行匹配,则结果输出多行。如果没有匹配行,则结果只输出一行,该输出左边为左表的一行的内容,右边全部输出null
  • 因为右边很可能出现有多行和左表的某一行匹配,所以左连接产生的结果集的行数很可能大于左边表的行数
select * from deptleft join empon dept.deptno=emp.deptno	--16行

返回一个事物和该事物的相关信息,如果没有相关信息,就输出空

右外连接

同左外连接

完全连接

full join

  • 两个表中匹配的所有行记录
  • 左表中那些在右表找不到匹配的行的记录,右边为NULL
  • 右表中那些在左表找不到匹配的行的记录,左边为NULL

交叉连接

cross join等价于join on 1=1,后面不用加on

自连接

一张表和自己连接起来,注意连接自己的时候需要标明是哪一张表中的字段

--求薪水最高的员工的信息select *from empwhere sal = (select MAX(sal) from emp)-- 不准用聚合函数,求薪水最高的员工的信息
select *from empjoin (select top 1 EMPNO from emp order by sal desc) "tmp"on emp.EMPNO=tmp.EMPNOselect *from empwhere sal not in (select distinct E1.salfrom emp as "E1"join emp as "E2"on E1.sal < E2.sal)

联合

纵向连接表中的数据,即添加一行

--输出每个员工的姓名,工资,上司的姓名select E1.ename as "姓名", E1.sal as "工资", E2.ename as "上司"from emp as "E1"left join emp as "E2"	--用左连接的原因是有一个没有上司on E1.mgr = E2.EMPNO--或者使用联合select E1.ename as "姓名", E1.sal as "工资", E2.ename as "上司"from emp as "E1"join emp as "E2"on E1.mgr = E2.EMPNO
union
select ename, sal, 'BOSS' from emp where  mgr is null

注意:

  • select子句输出列数相等
  • 数据类型也相同,至少是兼容的

分页查询

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383635.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下网络socket编程——实现服务器(select)与多个客户端通信

一、关于socket通信 服务器端工作流程&#xff1a; 调用 socket() 函数创建套接字 用 bind() 函数将创建的套接字与服务端IP地址绑定调用listen()函数监听socket() 函数创建的套接字&#xff0c;等待客户端连接 当客户端请求到来之后调用 accept()函数接受连接请求&#xff0c…

SQL Server【四】

identity 主键自动增长&#xff0c;用户不需要为identity修饰的主键赋值 create table student (std_id int primary key identity(10,5),--(10,5)可以省略&#xff0c;默认为(1,1)std_name nvarchar(200) not null ) select * from student insert into student values (张三…

TCP服务器/客户端实例(C/C )

1.1、Linux下的TCP服务器&#xff1a; #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h>void error_handling(char *mess…

pip代理解决pip下载失败问题

在用pip下载各种库的时候发现速度实在是太慢了&#xff0c;还会有各种奇奇怪怪的问题&#xff0c;动不动就玄学失败。 在网上找来找去找到知乎上一位大佬的回答&#xff1a;传送门&#xff0c;用了豆瓣的代理。哇咔咔&#xff0c;妈妈再也不用担心我下载失败了。 代理&#x…

实现Linux select IO复用C/S服务器代码

服务器端#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/stat.h> #include<arpa/inet.h> #include <sys/select.h>#define MAXBUF 256 #define MAXLISTEN…

Bellman-Ford算法和SPFA算法

Belloman-Ford算法 算法介绍 Dijkstra可以解决单源无负边最短路径问题。但是当遇到含有负边的单源最短路径问题就需要使用Bellman-Ford算法来解决。Bellman-Ford算法还可以检测出负环。 算法步骤 源点s,数组d[u]d[u]d[u]表示s到u的最短距离初始化&#xff1a;d[s]0d[s]0d[s…

C语言实现单链表操作

SLIST_H #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #include<assert.h> typedef int ElemType; typedef struct Node { //定义单链表中的结点信息 ElemType data; //结点的数据域 struct Node *next; //结点的指针…

计算机网络【4】传输层

概述 传输层是只有主机才有的层次 传输层的功能&#xff1a; 传输层提供进程和进程之间的逻辑通信&#xff08;网络层提供主机与主机之间的逻辑通信&#xff09;复用和分用传输层对收到的报文进行差错检测 传输层有两个协议&#xff1a; 面向连接的传输层控制协议TCP&…

Plotly绘图

在做Python数据分析实验的时候发现使用Plotly库绘图比较漂亮&#xff0c;在网上找到了一个比较好的教程&#xff0c;这里记录一下&#xff0c;方便以后查找。 传送门

计算机网络【0】概述

计算机网络概念和功能 概念 是一个将分散的、具有独立功能的计算机系统&#xff0c;通过通信设备与线路连接起来&#xff0c;由功能完善的软件实现资源共享和信息传递的系统。 计算机网络是互连的、自治&#xff08;无主从关系&#xff09;的计算机集合。 功能 数据通信&am…

计算机网络【1】物理层

物理层解决如何在连接各种计算机的传输媒体上传输数据比特流&#xff0c;而不是指具体的传输媒体。 确定与传输媒体接口有关的特性 机械特性&#xff1a;定义物理连接的特性&#xff0c;如规格、接口形状、引线数目、引脚数目、排列电气特性&#xff1a;规定传输二进制位时的电…

计算机网路【2】数据链路层

结点&#xff1a;主机、路由器 链路&#xff1a;两个节点的物理通道 数据链路&#xff1a;逻辑通道&#xff0c;把实现 控制数据传输协议的硬件和软件加到链路上就构成数据链路 帧&#xff1a;链路层的协议数据单元&#xff0c;封装网络层数据报 数据链路层在物理层提供服务的…

计算机网络【5】应用层

应用层对应用程序的通信提供服务 应用层协议定义&#xff1a; 应用层的功能&#xff1a; 文件传输、访问和管理电子邮件虚拟终端查询服务和远程作业登录 重要协议&#xff1a;FTP、SMTP、POP3、HTTP、DNS 网络应用模型 客户/服务器模型&#xff08;Client/Server&#x…

操作系统【八】文件管理

文件&#xff1a;一组有意义的信息/数据集合 文件的属性&#xff1a; 文件名&#xff1a;由创建文件的用户决定文件名&#xff0c;主要是为了方便用户找到文件。同一个目录下不允许有重名文件标识符&#xff1a;一个系统内的个文件标识符唯一&#xff0c;对用户来说毫无可读性…

数据库原理及应用【六】数据库设计

数据依赖 函数依赖FD&#xff1a;一个属性或者一组属性的值可以决定另一个属性的值 多值依赖MVD&#xff1a;一个属性或者一组属性的值可以决定另一个属性的值的集合。FD是MVD的特例 符号表示&#xff1a;Name->->Course&#xff0c;课程多值依赖于姓名 连接依赖&#x…

数据可视化【一】JavaScript学习

本博客是我学习Curran Kelleher老师数据可视化课程的笔记&#xff0c;感兴趣的小伙伴可以点击这里学习。 three cores of data visualization: analysisdesignconstruction 推荐书籍《visualization analysis & design》 使用https://vizhub.com/进行编程学习&#xff…

数据库原理及应用【二】数据模型

层次模型 tree Record and fieldParent-Child relationship(PCR) 每个记录类型只有一个父节点 无法表达多对多信息 采用虚记录解决多对多 网状数据模型 系&#xff1a;主记录->属记录 主记录和属记录都可以有好多个 关系模型 表&#xff1a;table/relation 拥有更高的…

数据可视化【二】HTML+CSS+SVG+D3

HTML、CSS和SVG学习实现代码&#xff1a;https://vizhub.com/Edward-Elric233/89185eb96bc64a9d81777873a0ccd0b9 index.html <!DOCTYPE html> <html><head><title>Shapes with SVG and CSS</title><link rel"stylesheet" href&qu…

数据可视化【三】基本概念

Visualization is suitable when there is a need to augment human capabilities rather than replace people with computational decision-making methods. 当可以信赖的智能化的解决方案存在的时候&#xff0c;可视化是不必要的。 当不知道需要分析的问题是什么的时候&…

数据可视化【四】Bar Chart

Make a Bar Chart Representing a data table in JavaScriptCreating rectangles for each rowUsing linear and band scalesThe margin conventionAdding axes 以下学习内容参考博客&#xff1a;传送门 select()选择所有指定元素的第一个 selectAll()选择指定元素的全部 上…