zoukankan      html  css  js  c++  java
  • left join 过滤条件写在on后面和写在where 后面的区别

    create table t1(id int, feild int);
    insert into t1 values(1 , 1);
    insert into t1 values(1 , 2);
    insert into t1 values(1 , 3);
    insert into t1 values(1 , 4);
    insert into t1 values(2 , 1);
    insert into t1 values(2 , 2);
    create table t2(id int, feild int);
    insert into t2 values(1 , 1);
    insert into t2 values(1 , 2);
    insert into t2 values(1 , 5);
    insert into t2 values(1 , 6);
    insert into t2 values(2 , 1);
    insert into t2 values(2 , 3);

    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id   --取t1表的第一行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
                                                                          然后取t1表的第二行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
                                                                          重复以上过程,直到t1表扫描结束.

    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  and t1.feild=1  --给左表加条件的时候,左表满足条件的,按上面的过程返回值,左表不满足条件的,直接输出,右表的列补null
                                                                                                  1 1 1 1
                                                                                                  1 1 1 2
                                                                                                  1 1 1 5
                                                                                                  1 1 1 6
                                                                                                  2 1 2 1
                                                                                                  2 1 2 3                   
                                                                                                  1 2 NULL NULL
                                                                                                  1 3 NULL NULL
                                                                                                  1 4 NULL NULL
                                                                                                  2 2 NULL NULL


    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  where t1.feild=1     先执行where后连接查询

                                                                                        执行where后表为  1 , 1
                                                                                                                2 , 1
                                                                                              用它来left join t2.
     

     --下面三条语句查询结果是一样的,当为右表加条件的时候,可以把left join 改为inner jin, 因为inner join比left join 要快!

    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  and t2.feild=1
    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  where t2.feild=1
    select t1.*,t2.* from t1 inner join t2 on t1.id=t2.id  and t2.feild=1

  • 相关阅读:
    unity小记
    Animator 设置动画效果
    Animation(动画效果)
    蜜蜂游戏 小结
    unity 协同
    camera render texture 游戏里的监控视角
    Mybatis框架 第一天
    BOS项目 第12天(总结)
    BOS项目 第11天(activiti工作流第三天,流程实例管理、项目中的用户和角色同步到activiti的用户和组表、设计物流配送流程、启动物流配送流程、组任务操作(查询、拾取)、个人任务操作(查询、办理))
    BOS项目 第10天(activiti工作流第二天,流程变量、组任务、排他网关、spring整合activiti、项目中实现流程定义管理)
  • 原文地址:https://www.cnblogs.com/pan11jing/p/1543443.html
Copyright © 2011-2022 走看看