zoukankan      html  css  js  c++  java
  • SQL Server join介绍

    介绍Inner Join(可以省略Inner,平常经常inner,就是inner join), Full Out Join,Cross Join,Left Join, Right Join区别。

    create table Customers (Cust_Id int, Cust_Name varchar(10))
    insert Customers values (1, 'Craig')
    insert Customers values (2, 'John Doe')
    insert Customers values (3, 'Jane Doe')
     
    create table Sales (Cust_Id int, Item varchar(10))
    insert Sales values (2, 'Camera')
    insert Sales values (3, 'Computer')
    insert Sales values (3, 'Monitor')
    insert Sales values (4, 'Printer')
    Customers 表数据:

    Sales 表数据:


    1、inner join
    两边都有的才筛选出来(Inner join 对表无顺序概念)
    --Inner Join
    --两边都有的才筛选出来(Inner join 对表无顺序概念)
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from Sales S inner join Customers C
    on S.Cust_Id = C.Cust_Id
    
    --平时经常就简单使用单使用join字段,就是inner join
    --如下实例: 结果集跟使用inner join 一模一样
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from Customers c join sales s
    on c.cust_id = s.cust_id

    2、Full Out Join

    两边都列出来,能匹配就匹配,不匹配的用NULL列出 (Full Out Join 对表无顺序概念)
    --Full Out Join
    --两边都列出来,能匹配就匹配,不匹配的用NULL列出 (Full Out Join 对表无顺序概念)
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from Sales S full outer join Customers C
    on S.Cust_Id = C.Cust_Id

    3、Cross Join

    列出两边所有组合,也叫笛卡尔集A.Rows * B.Rows (Cross Join 对表无顺序概念)
    --Cross Join
    --列出两边所有组合,也叫笛卡尔集A.Rows * B.Rows (Cross Join 对表无顺序概念)
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from Sales S cross join Customers C

    4、Left Join

    以左边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
    Left Join 对表有顺序概念  前面是主表 后面是副表
    --Left Join
    --以左边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
    --Left Join 对表有顺序概念  前面是主表 后面是副表
    -- 实例-1、Sales 为主表 (两个表的数据都显示)
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from sales s left join Customers c 
    on c.cust_id = s.cust_id

    2)、Customers 为主表

    -- 实例-2、Customers 为主表
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from  Customers c left join sales s
    on c.cust_id = s.cust_id

    5、Right Join

     以右边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
     Right Join 对表有顺序概念 前面是副表 后面是主表

    --Right Join
    --以右边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
    --Right Join 对表有顺序概念  前面是副表 后面是主表
    -- 实例.1、 sales 为主表
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from  sales s right join Customers c 
    on c.cust_id = s.cust_id

    2)、Salves为主表

    -- 实例.2、 sales 为主表
    select 
    s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
    C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
    from Customers c right join sales s
    on c.cust_id = s.cust_id



  • 相关阅读:
    679 怎样杀死害虫?(对付一个系统最好的方式是“围城必阙”)
    678 "流浪地球"为什么是个好地方?(系统越复杂拥有好运气的机会也就越大)
    677 人类为什么会养猫?(做一件事理性的原因的背后往往还隐藏着自己都不曾发现的感性原因)
    职场人必知的三原则
    677 怎样当一个少数派?(越在意,越出众)
    675 为什么会有“黑天鹅”?(行为和对行为后果的负责与否决定了很多黑天鹅出现概率)
    不做特殊论者(没有所谓的理所当然,你所谓的成功很有可能只是因为运气)
    事实和观点(就事论事,事实有真假,观点无对错)
    一个程序员的价值观总结
    669 创新也是搞政治?(如何创新)
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/6185999.html
Copyright © 2011-2022 走看看