Consulta de conexión de SQLserver

Conexión interna

--连接查询
--查询客户名称,采购数量,采购单价,商品名称

--方法一:内连接:inner join 语法:  select 列名1,列名2... from 表名1 inner join 表名2 on (外键表.外键=主键表.主键) 
--查询客户名称,采购数量,采购单价,商品名称
select clientName,productNumber,salePrice,productName from sales inner join product on(sales.productId=product.productId)
--查询客户名称,商品名称,商品报价
select clientName,productName,price from sales s inner join  product p on(s.productId=p.productId)
--方法二:通过等值连接实现内连接查询: select 列名1,列名2... from 表名1,表名2 where 外键表.外键=主键表.主键
select clientName,productName,price from sales s,product p where s.productId=p.productId

--查询单笔销售数量>20的商品名称,销售单价和销售数量
select productName,salePrice,productNumber from product inner join sales on(product.productId=sales.productId) where productNumber>20
select productName,salePrice,productNumber from product,sales where product.productId=sales.productId and productNumber>20

--empdb
use empdb
select * from dept
select * from emp
--查询员工姓名,性别,地址及部门名称和部门人数
select empName,empSex,empAddress,deptName,deptNum from emp inner join dept on(emp.deptId=dept.deptId)
--查询姓名中出现"凯歌"的员工姓名,性别,地址及部门名称和部门人数
select empName,empSex,empAddress,deptName,deptNum from emp inner join dept on(emp.deptId=dept.deptId) where empName like '%凯歌%'
select empName,empSex,empAddress,deptName,deptNum from emp,dept where emp.deptId=dept.deptId and empName like '%凯歌%'

Unión externa

----外连接
select * from product
select * from sales

--左连接:left join(左边的表中的记录全部出现而右边只出现与左表相关联数据)
select productName,clientName,productNumber from product inner join sales on(product.productId=sales.productId)

select productName,clientName,productNumber from product left join sales on(product.productId=sales.productId)

select productName,clientName,productNumber from sales left join product  on(product.productId=sales.productId)

--右连接:right join(右边的表中的记录全部出现而左边只出现与右表相关联数据)
select productName,clientName,productNumber from product right join sales on(product.productId=sales.productId)

select productName,clientName,productNumber from sales right join  product on(product.productId=sales.productId)
 
--全连接:full join(左右表中的数据都出现)
select productName,clientName,productNumber from sales full join  product on(product.productId=sales.productId)

--交叉连接:cross join   不用带后面的条件(出现的记录数是左右表中记录相乘)
select productName,clientName,productNumber from sales cross join  product
  
use empdb
select * from emp
select * from dept
--内连接
select deptName,empName from emp inner join dept on(emp.deptId=dept.deptId)

--左外连接
select deptName,empName from emp left join dept on(emp.deptId=dept.deptId)

--右外连接
select deptName,empName from emp right join dept on(emp.deptId=dept.deptId)

--全连接
select deptName,empName from emp full join dept on(emp.deptId=dept.deptId)

--交叉连接(笛卡尔成绩):cross join (两个表中的记录数相乘既是总的出现的条数)
select * from emp
select * from dept
select deptName,empName from emp cross join dept
select productName,clientName from product left join sales on(product.productId =sales.productId) 
select productName,clientName from product right join sales on(product.productId =sales.productId)  
select productName,clientName from product cross join sales 

Subconsulta 

--子查询和连接查询
--子查询 :嵌套在 select insert update delete 语句中的select语句查询
--注意 :写子查询时
select * from product
select * from sales

--查询采购数量大于20的商品名称
--1.查询采购数量>20的商品编号
select productId from sales where productNumber>20  --1,2,3
--2.根据商品编号查询商品名称
select productName from product where productId in(1,2,3)
--子查询
select productName from product where productId in(select productId from sales where productNumber>20)

--查询采购总数量>50的商品名称
--1.查询采购总数量>50的商品编
select productId from sales group by productId having SUM(productNumber)>50
--2.根据商品编号查询商品名称
select productName from product where productId in(select productId from sales group by productId having SUM(productNumber)>50)

select * from product
select * from sales
select productId  from sales where productNumber >20
select productName                                                                                                        select * from product
select * from sales
--查询"某技术学院"所采购的商品名称
--1.根据"某技术学院"查询所采购的商品编号
select productId from sales where ClientName='某技术学院'
--2.根据商品编号查询商品名称
select productName from product where productId in(1,2)
select productName from product where productId in(select productId from sales where ClientName='某技术学院')

--查询采购数量最多的商品名称
--1.查询采购数量最多的数目:40
select MAX(productNumber) from sales
--2.查询采购数量为40的商品编号
select productId from sales where productNumber=40
--3.查询编号为3的商品名称
select productName from product where productId=3
--通过子查询
select productName from product where productId=(select productId from sales where productNumber=(select MAX(productNumber) from sales))

--查询已采购的商品名称
--1.查询所有被销售的商品编号(销售表)
select distinct productId from sales 
--2.根据商品编号查询商品名称(商品表)
select productName from product where productId in(1,2,3)
select productName from product where productId in(select distinct productId from sales)
--查询未被采购的商品名称
--1.查询所有被销售的商品编号(销售表)
select distinct productId from sales 
--2.根据商品编号查询商品名称(商品表)
select productName from product where productId not in(1,2,3)
select productName from product where productId not in(select distinct productId from sales )
               

 

Supongo que te gusta

Origin blog.csdn.net/weixin_48135624/article/details/115211100
Recomendado
Clasificación