ID #64960

浅谈join 用法

join 分为如下几种:

分为1. INNER JOIN 2. LEFT JOIN   3. RIGHT JOIN 4.FULL OUTER JOIN

首先设定一个我们要用的两个表

                             表A                                                          表B

               ID                     NAME                                ID                 CLASS             

                 1                       IBM                                    1                    C1

                 2                     SONY                                 3                    C3

                 3                      BMW                                  4                     C4

1 INNER JOIN:          SELECT * FROM   A      INNER JOIN B ON A.ID=B.ID

得表                       ID                NAME          CLASS

                                1                   IBM                C1

                               3                 BMW               C3           

就是 两个表的ID都存在并相同 得到这两个表的组合表

2 LEFT JOIN :          SELECT * FROM   A      LEFT JOIN B ON A.ID=B.ID

得表                       ID                NAME          CLASS

                                1                   IBM                C1

                                2                   SONY             null

                               3                 BMW               C3           

   3   RIGHT JOIN :          SELECT * FROM   A      RIGHT JOIN B ON A.ID=B.ID

得表                       ID                NAME          CLASS

                                1                   IBM                C1

                                3                BMW               C3

                                4                 null                 C4           

对了RIGHT(右)   得到的是B(right语句的右边的表)的所有记录 而A表对应的记录没有的话也要补齐   (我也翻身了! 呵呵)

4 FULL OUTER JOIN:    SELECT * FROM   A      FULL OUTER JOIN B ON A.ID=B.ID

得表                       ID                NAME          CLASS

                                1                   IBM                C1

                               2                 SONY            null

                                3                BMW               C3

                                4                 null                 C4           

得到的是A和B的所有记录 而双方对应的记录没有的话也要补齐


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wangfou/archive/2009/12/20/5044868.aspx

2011-08-24 19:43
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

sql中all,any,some用法

sql语句查询结果合并union all用法_数据库技巧

SQL中 getdate() 用法

Sql server STUFF的用法

sql语句查询结果合并union 和union all用法