ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQLServer第四章:多表查询

2022-09-16 03:00:48  阅读:223  来源: 互联网

标签:cardInfo 多表 -- SQLServer PCId select join PCInfo 第四章


多表查询是SQL数据库里的高级查询语句。

创建3个数据库,做演示。

create database NetBarDB    --创建数据库create database 数据库名
go    --批处理(数据库无法自动运行下一句代码,需要加go来继续代码的运行)
use NetBarDB                --打开数据库
go

if exists(select * from sys.objects where name='PCInfo')
    begin
        drop table PCInfo
    end

create table Stufo             
(
    StuId int primary key identity, 
    StuName char(10) unique not null,      --姓名,约束:unique【唯一的】和主键一样有唯一标识,表示内容不能有重复数据,not null不为空;                 
)
create table PCInfo             
(
    PCId int primary key identity,                     
    PCScore int not null,   --成绩
    PCScore2 int    --成绩
)

create table cardInfo            
(
    id int primary key identity,   
    age int not null ,   --年龄
    sex char(2),         --性别     
)

insert into Stufo values('张三'),('李四'),('王五')
insert into PCInfo values(5,40),(10,70),(60,100)
insert into cardInfo values(18,'男'),(19,'女'),(20,'男')

select * from Stufo
select * from PCInfo
select * from cardInfo

 多表高级查询语句。

--联合查询:  select * from 表1,表2,表n  where   条件1  and  条件2  and 条件n
select * from Stufo,PCInfo,cardInfo  where StuId=PCId and PCId=id --两个表只需要一个条件就可以,and后面的条件可以不用

--联接查询:join  注意:返回的数据,与另一个表没有关联数据时,都是NULL
--内连接:inner join   返回:两个表的公共部份数据(交集),不匹配的数据直接不显示。
select * from PCInfo inner join cardInfo on PCInfo.PCId=cardInfo.age 
select StuName as 姓名,
        cardInfo.age as 年龄,
        cardInfo.sex as 性别,
        PCInfo.PCScore as 成绩
from PCInfo inner join Stufo  on PCInfo.PCId=Stufo.StuId inner join cardInfo on PCInfo.PCId = cardInfo.id
--左连接:left join        返回:左表所有数据,及与右表公共部份数据
select * from PCInfo left join cardInfo on PCInfo.PCScore=cardInfo.id 
--右连接:right join   返回:右表所有数据,及与左表公共部份数据
select * from PCInfo right join cardInfo on PCInfo.PCScore=cardInfo.id 
--外连接:full join        返回:两个表的所有数据
select * from PCInfo full join cardInfo on PCInfo.PCId=cardInfo.age 
--扩展
select top 1 * from PCInfo inner join cardInfo on PCInfo.PCId=cardInfo.id order by PCInfo.PCScore desc --根据成绩排序,查成绩第一名
select top 2 StuName 姓名,(PCScore+PCScore2)/2 as 成绩                                            
    from PCInfo,Stufo where PCInfo.PCId=Stufo.StuId order by ((PCScore+PCScore2)/2) desc   --平均分最高的两个人

--并集union:去掉重复数据,把两个表数据叠加在一起
select COUNT(*) from PCInfo where PCScore2=100
union
select COUNT(*) from cardInfo where sex='女'
--并集union all:不会去掉重复
select COUNT(*) from PCInfo where PCScore2=100
union all
select COUNT(*) from cardInfo where sex='女'
--交集intersect:两个表相同的数据显示出来
select PCInfo.PCScore from PCInfo
intersect 
select cardInfo.id from cardInfo
--差集except:自动删除两表重复行。并且只显示表1,不显示表2,表1在表2有的数据也不显示
select PCScore from PCInfo
except 
select cardInfo.id from cardInfo

 

标签:cardInfo,多表,--,SQLServer,PCId,select,join,PCInfo,第四章
来源: https://www.cnblogs.com/longxinyv/p/16698549.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有