`
jackleechina
  • 浏览: 571526 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

join......on 后面的and 和where

    博客分类:
  • sql
 
阅读更多
运行平台:Mysql
目的:比较join......on 后面的and 和where的区别

1)建表
创建A表
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `a`
-- ----------------------------
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
  `id` decimal(10,0) NOT NULL DEFAULT '0',
  `link` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `a_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES ('1', '1', 'jack');
INSERT INTO `a` VALUES ('2', '1', 'jack');
INSERT INTO `a` VALUES ('3', '1', 'leo');


创建B表
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `b`
-- ----------------------------
DROP TABLE IF EXISTS `b`;
CREATE TABLE `b` (
  `id` decimal(10,0) NOT NULL DEFAULT '0',
  `link` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
  `b_name` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of b
-- ----------------------------
INSERT INTO `b` VALUES ('1', '1', 'frowna');
INSERT INTO `b` VALUES ('2', '1', 'frowna');
INSERT INTO `b` VALUES ('3', '1', 'kiki');


2)比较下面的运行结果

select * from A left join B on A.link =B.link where A.a_name='jack'
和
select * from A left join B on A.link =B.link and A.a_name='jack'


select * from A inner join B on A.link =B.link where A.a_name='jack'
和
select * from A inner join B on A.link =B.link and A.a_name='jack'


select * from A right join B on A.link =B.link where A.a_name='jack'
和
select * from A right join B on A.link =B.link and A.a_name='jack'


select * from A right join B on A.link =B.link where A.a_name is null
和
select * from A right join B on A.link =B.link and A.a_name is null 


3)结论
3.1) where 是在两个表join完成后,再附上where条件。

select * from (select A.a_name,B.b_name from A left join B on A.link =B.link)t where t.a_name='jack';
等价为
select A.a_name,B.b_name from A left join B on A.link =B.link  where A.a_name='jack'


3.2)
而 and 则是在表连接前过滤A表或B表里面哪些记录符合连接条件,同时会兼顾是left join还是right join。即
假如是左连接的话,如果左边表的某条记录不符合连接条件,那么它不进行连接,但是仍然留在结果集中(此时右边部分的连接结果为NULL)。

on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。参考http://blog.csdn.net/muxiaoshan/article/details/7617533

3.3)建议尽量用where来过滤条件,以避免复杂的逻辑考虑。(除非在某些情况下(后接其他sql语句),用and会报错,才用and,但要考虑是否影响正确结果。)

分享到:
评论

相关推荐

    c#操作数据库,史上最牛逼的方法,你见过这种方法吗?

    InnerJoin(b).On(a.UserId == b.CategoryId) .Where(a.UserId == 1 && (a.UserName + "123").LikeLeft(u1.UserName)) ); //下面是子查询方式的多表查询In 的使用方法 var ListUser8= Sql.ExecuteList((a, b, c) =>...

    精通SQL数据库连接.doc

    WHERE coname LIKE ‘%Tech%’ AND indname = ‘Computing’; 注意AS关键字是任选的,然而我建议使用他从而更加清楚。而且,专栏可以使用格式alias = table_name.column_name来别名化,但表格不能这样来别名化。 自...

    C#语法及存储过程.docx

    select * from table where NAME = @NAME and AGE = @AGE end exec user @NAME = '男',@AGE = 15 select 单号 ,收入 = case when 收入>0 then 收入 else 0 end ,支出 = case when 收入>0 then 0 else ...

    Oracle练习笔试大全

    Oracle练习笔试大全 1、select ename, sal * 12 from emp; //计算年薪 2、select 2*3 from dual; //计算一个比较纯的数据...68、select ename,grade from emp e join salgrade s on(e.sal between s.losal and s.hisal...

    T-SQL高级查询

    select * from student where id not between 2 and 5; --like 模糊查询 select * from student where name like '%a%'; select * from student where name like '%[a][o]%'; select * from student where name ...

    mysql数据库操作

    1、关联查询 select students.name,class.name from class inner join students on class.id = students.classid; 2、left join 会将A中有的填充到左边,没有用null填充 3,rightjoin 会将b中有填充到右边,没有用...

    经典SQL语句大全

    例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 4、说明:子查询(表名1:a 表名2:b) select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3) 5、...

    数据库操作语句大全(sql)

    例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 4、说明:子查询(表名1:a 表名2:b) select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3) 5、...

    sql经典语句一部分

    例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 4、说明:子查询(表名1:a 表名2:b) select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3) 5、...

    MySQL命令大全

    在进行开发和实际应用中,用户不应该只用root用户进行连接数据库,虽然使用root用户进行测试时很方便,但会给系统带来重大安全隐患,也不利于管理技术的提高。我们给一个应用中使用的用户赋予最恰当的数据库权限。如...

    MYSQL常用命令大全

    在进行开发和实际应用中,用户不应该只用root用户进行连接数据库,虽然使用root用户进行测试时很方便,但会给系统带来重大安全隐患,也不利于管理技术的提高。我们给一个应用中使用的用户赋予最恰当的数据库权限。如...

    经典全面的SQL语句大全

     例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..  4、说明:子查询(表名1:a 表名2:b) select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3) ...

    精髓Oralcle讲课笔记

    68、select ename,grade from emp e join salgrade s on(e.sal between s.losal and s.hisal); --两张表的连接 此种写法比用where更清晰 69、select ename, dname, grade from emp e join dept d on(e.deptno =...

    SQL培训第一期

    from student_A A full outer join student_B B on A.Uuid = B.Uuid; 1.6.4.3 结果 1.6.4.4 全外连接不支持(+)写法 1.6.5 (+) + 表示补充,即哪个表有加号,这个表就是匹配表。 1.7 运算符 1.7.1 比较 =、>,<,...

    jpivot学习总结.doc

    baseDisplayURL 否 String 是 显示图表的链接,链接的后面还要添加参数“ ?=filename=[ 临时图表文件的名称 ] ” controllerURL 否 String 是 链接到 JPivot Controller 的 URL ,该属性在一些复杂环境下...

    springmybatis

    要保持一致,当然这里的 resultType 还有另外单独的定义方式,后面再说。 2. Configuration.xml 里面 的<mapper resource="com/yihaomen/mybatis/model/User.xml"/>是包含要映射的类的xml配置文件。 3. 在User.xml ...

    MySql基本查询、连接查询、子查询、正则表达查询讲解

    select * from STUDENT where STU_AGE between 13 and 15; (2)逻辑运算符 not ( ! ) 逻辑非 [sql] view plain copy select * from STUDENT where STU_AGE NOT IN(13,14,16); or ( || ) 逻辑或 OR关键字也...

    mysql数据库的基本操作语法

    级联删除:删除主表的数据时,关联的从表数据也删除,则需要在建立外键约束的后面增加on deletecascade 或on delete set null,前者是级联删除,后者是将从表的关联列的值设置为null。 create table student( id int...

    SQL语法大全

    sql="select * from 数据表 where 字段名 between 值1 and 值2" (2) 更新数据记录: sql="update 数据表 set 字段名=字段值 where 条件表达式" sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    日期类型 date 7字节 用于存储表中的日期和时间数据,取值范围是公元前4712年1月1日至公元9999年12月31日,7个字节分别表示世纪、年、月、日、时、分和秒 二进制数据类型 row 1~2000字节 可变长二进制数据,在具体...

Global site tag (gtag.js) - Google Analytics