浅谈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的所有记录 而双方对应的记录没有的话也要补齐 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值