茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672195 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00177.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼]MSSQL Join總結

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]MSSQL Join總結
MSSQL Join總結

SQL 之JOIN 用法完全版一、各種JOIN的含義SQL中大概有這麼幾種JOIN:
cross join
inner join
left outer join
right outer join
full outer join

首先都是基於cross join(笛卡爾乘積),然後是inner join,在笛卡爾乘積的結果集中去掉不符合連接條件的行。
left outer join 是在inner join的結果集上加上左表中沒被選上的行,行的右表部分每個字段都用NUll填充。
right outer join 是在inner join的結果集上加上右表中沒被選上的行,行的左表部分全用NULL填充。
outer的意思就是"沒有關聯上的行"。

二、舊式寫法和標準寫法:
1、INNER Join code as the following:
Select * from A a, B b where a.categoryID = b.categoryID;

Equals:
Select * from A a inner join B b on a.categoryID = b.categoryID;

2、OUTER Join code as the following:
select * from A a full(left/right) outer  join B b  on a on a.categoryID = b.categoryID;

Equals:
Select * from A a, B b where a.categoryID *=  b.categoryID;Select * from A a, B b where a.categoryID =*  b.categoryID;


三、例子Table A have 12( 8+4) entries, 8 entries have valid relation with BTable B have 80(77+3) entries , 77 entries have valid relation with A. then the return amount of join is :
cross join : 12*80
inner join : 77
full outer join : 77+4+3
left outer join: 77 + 4
right outrer join: 77 + 3

join 方式
(1) cross join
參與select語句所有表的的所有行的笛卡爾乘積
      select au_lname ,title from authors cross join  titiles

outer join 對參與join的兩個表有主從之分,處理方式以主表的每條數據去match 從屬表的列,合乎條件的數據是我們所要的答案,不合乎條件的也是我們要的答案,只不過哪些從屬表選取的列將被添上null。
(2) left join
左邊的為主表,右邊為從屬表
     select a.cust_id ,b.order_date,b.tot_ant from customer a left join sales b
       on (a.cust_id =b.cust_id and b.order_date>''1996/10/15'')

可以寫為
     select a.cust_id,b.order_date,b.tot_ant from custom a
        left join (select * from sales where order_date>''1996/10/15'') b
        on a.cust_id =b.cust_id

(3) right join
左邊的表為從屬表,右邊的表為主表
(4) self join
self join 常用在同一表內不同數據間對同一列的比較
     select a.emp_no,a.emp_name,b.emp_no,b.emp_name,a.date_hired from employee a
        join employee b on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired)
            order by a.date_hired

這樣會重複數據,只要加上一句
and a.emp_name>b.emp_name

(5) full join
不僅列出符合條件的數據,兩邊未符合join條件的數據也會一併列出。哪些未符合join條件的數據如果在select列中無法得到對應的值則填上null
    select a.cust_id,b.tot_amt from customer a full join sales b
         on a.cust_id=b.cust_id

有表
     id   ic  name  amount
      I    *        *
      c
      i
      c
      i
      i

要求結果為
    ic  name  amount  ic   name  amount
    i                         c
    i                         c
    i
    i

  select aaa.*,bbb.*  from (select (select count(id) from aa as b where (b.id<a.id) and (ic=''i'')) as newid, *
        from aa a where ic=''i'') aaa full join
         (select (select count(id) from aa as b where b.id<a.id and ic=''c'') as newid,* from
             aa a where ic=''c'') bbb on aaa.newid=bbb.newid order by aaa.name

6.使用 HASH 和 MERGE 聯接提示
此示例在 authors、titleauthors 和 titles 表之間建立三表聯接,以生成一個作者及其著作的列表。查詢優化器使用 MERGE 聯接將 authors 和 titleauthors (A x TA) 聯接在一起。然後,將 authors 和 titleauthors MERGE 聯接 (A x TA) 的結果與 titles 表進行 HASH 聯結以生成 (A x TA) x T。
重要 指定聯接提示後,要執行 INNER JOIN 時 INNER 關鍵字不再為可選,而必須顯式說明。
  USE pubs
  SELECT SUBSTRING((RTRIM(a.au_fname) + '' '' + LTRIM(a.au_lname)), 1, 25)
     AS Name, SUBSTRING(t.title, 1, 20) AS Title
     FROM authors a INNER MERGE JOIN titleauthor ta
      ON a.au_id = ta.au_id INNER HASH JOIN titles t
      ON t.title_id = ta.title_id
     ORDER BY au_lname ASC, au_fname ASC

下面是結果集:
  Warning: The join order has been enforced because a local join hint is used.
  Name                      Title
  ------------------------- --------------------
  Abraham Bennet            The Busy Executive''s
  Reginald Blotchet-Halls   Fifty Years in Bucki
  Cheryl Carson             But Is It User Frien
  Michel DeFrance           The Gourmet Microwav
  Innes del Castillo        Silicon Valley Gastr
  ...                    ...
  Johnson White             Prolonged Data Depri
  Akiko Yokomoto            Sushi, Anyone?

  (25 row(s) affected)

                      dbo.CheckPerson.checkPersonID = dbo.Employee.employeeID INNER JOIN
                      dbo.Employee AS Employee_1 ON dbo.RequirePlan.establishPersonID = Employee_1.employeeID AND
                      dbo.CheckPerson.checkPersonID = Employee_1.employeeID

Employee_1前邊沒有dbo.

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luxuejuncarl/archive/2006/12/31/1470612.aspx
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|