首页 > 开发 > 综合 > 正文

浅谈join 用法

2024-07-21 02:45:08
字体:
来源:转载
供稿:网友
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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表