join on 与 where 条件的执行先后顺序:

  join on 条件先执行,where条件后执行;join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤

left join、right join、full join、inner join区别:

  left join:以左表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对左表无效

  right join:以右表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对右表无效

  full join:以左表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对左右表无效

  inner join:等值连接,根据过滤条件生成临时表。用inner join 后面的条件 可以用 where实现

  

  where:对生成的临时表进行过滤,inner join能完成的功能用where条件都可以完成,但反之则不是啦。

建表语句:

 CREATE TABLE `t_salecategory_product_relation` (
`relation_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键列',
`product_id` int(11) NOT NULL COMMENT '商品ID,外键',
`product_code` varchar(32) NOT NULL COMMENT '商品编码',
`category_id` bigint(20) NOT NULL COMMENT '运营分类ID,外键,对应表t_sale_category中的主键列',
`category_code` varchar(64) NOT NULL COMMENT '运营分类编号',
`order_value` int(11) DEFAULT NULL COMMENT '排序值,在搜索时使用,按降序排',
`mount_type` smallint(6) NOT NULL COMMENT '挂载类型:\r\n 1:自动挂载;\r\n 2:手动挂载\r\n ',
`opt_type` smallint(6) DEFAULT NULL COMMENT '操作类型: 1 更新 2 删除 ,默认为1',
`mount_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '挂载时间',
`mount_user` varchar(64) DEFAULT NULL COMMENT '挂载人',
`last_update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '最后修改时间',
`last_update_user` varchar(64) DEFAULT NULL COMMENT '最后修改人',
PRIMARY KEY (`relation_id`),
UNIQUE KEY `IDX_productcode_salecode` (`product_code`,`category_code`),
KEY `FK_product_saleCategory` (`category_id`),
KEY `FK_salCatProduct_prdInfo` (`product_id`),
CONSTRAINT `FK_salCatProduct_prdInfo` FOREIGN KEY (`product_id`) REFERENCES `t_product` (`product_id`) ON DELETE CASCADE,
CONSTRAINT `FK_FK_saleCategory_saleCategoryProductRelation` FOREIGN KEY (`category_id`) REFERENCES `t_sale_category` (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=106 DEFAULT CHARSET=utf8 COMMENT='运营分类和商品挂载关系表';
 CREATE TABLE `t_product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '产品ID',
`product_name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`product_code` varchar(32) DEFAULT NULL COMMENT '商品编码',
`product_desc` varchar(512) DEFAULT NULL COMMENT '商品描述',
`product_shelves` int(1) DEFAULT NULL COMMENT '商品上下架状态',
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
`create_by` int(11) DEFAULT NULL COMMENT '创建人',
`create_user_name` varchar(255) DEFAULT NULL,
`update_time` int(11) DEFAULT NULL COMMENT '最后修改时间',
`update_by` int(11) DEFAULT NULL COMMENT '最后修改人',
`update_user_name` varchar(255) DEFAULT NULL,
`first_shelves` int(11) DEFAULT NULL COMMENT '第一次上架人ID',
`first_shelves_name` varchar(32) DEFAULT NULL COMMENT '第一次上架 人名称',
`first_shelves_time` int(11) DEFAULT NULL COMMENT '第一次上架时间',
`last_shelves` int(11) DEFAULT NULL COMMENT '最后一次上架人ID',
`last_shelves_name` varchar(32) DEFAULT NULL COMMENT '最后一次上架人名称',
`last_shelves_time` int(11) DEFAULT NULL COMMENT '最后一次上架时间',
`down_shelves` int(11) DEFAULT NULL COMMENT '最后一次下架人ID',
`down_shelves_name` varchar(32) DEFAULT NULL COMMENT '最后一次下架人名称',
`down_shelves_time` int(11) DEFAULT NULL COMMENT '最后一次下架时间',
`cost_price` double DEFAULT NULL COMMENT '成本价',
`tsh_price` double DEFAULT NULL COMMENT '销售价',
`tb` int(11) DEFAULT NULL COMMENT '特币',
`market_price` double DEFAULT NULL COMMENT '市场价',
`brand_code` varchar(16) DEFAULT NULL COMMENT '基础品牌编码',
`brand_name` varchar(64) DEFAULT NULL COMMENT '基础品牌名称',
`cat_code` varchar(16) DEFAULT NULL COMMENT '基础分类编码',
`cat_name` varchar(64) DEFAULT NULL COMMENT '基础分类名称',
`type` int(11) DEFAULT NULL COMMENT '类型',
`staus` int(1) DEFAULT NULL COMMENT '状态',
`main_pic` varchar(255) DEFAULT NULL COMMENT '主图',
`supplier_id` int(11) DEFAULT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=142786916 DEFAULT CHARSET=utf8 COMMENT='商品基本属性表';

采用 inner join 过滤 左表

 SELECT
t1.relation_id,
t1.product_id,
t1.product_code,
t2.product_id AS p_product_id,
t2.product_name AS p_product_name,
t2.product_code AS p_product_code,
FROM
t_salecategory_product_relation t1
JOIN
t_product t2 ON t1.product_id = t2.product_id and t1.category_id = 1

使用where 语句过滤,理论上效率应该比 inner join 低,未测试过。。。

 SELECT
t1.relation_id,
t1.product_id,
t1.product_code,
t2.product_id AS p_product_id,
t2.product_name AS p_product_name,
t2.product_code AS p_product_code,
FROM
t_salecategory_product_relation t1
LEFT JOIN
t_product t2 ON t1.product_id = t2.product_id
WHERE
t1.category_id = 1;

错误的语句,左连接left join 时对 左表的过滤失效,即 t1.category_id = 1 条件不起效

 SELECT
t1.relation_id,
t1.product_id,
t1.product_code,
t2.product_id AS p_product_id,
t2.product_name AS p_product_name,
t2.product_code AS p_product_code,
FROM
t_salecategory_product_relation t1
LEFT JOIN
t_product t2 ON t1.product_id = t2.product_id and t1.category_id = 1

数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别的更多相关文章

  1. 详解mysql数据库的左连接、右连接、内连接的区别

    一般所说的左连接,外连接是指左外连接,右外连接.做个简单的测试你看吧. 先说左外连接和右外连接: SQL>select * from t1; ID NAME ---------- ------- ...

  2. SQL的几种连接:内连接、左联接、右连接、全连接、交叉连接

    SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:             book表                                          stu表 1.内连接 ...

  3. Sql中的内连接,左连接以及右连接区别

    转自:http://pangaoyuan.javaeye.com/blog/713177 有两个表A和表B. 表A结构如下: Aid:int:标识种子,主键,自增ID Aname:varchar 数据 ...

  4. SQL 左外连接,右外连接,全连接,内连接

    原文地址  连接条件可在FROM或WHERE子句中指定,建议在FROM子句中指定连接条件.WHERE和HAVING子句也可以包含搜索条件,以进一步筛选连接条件所选的行.             连接可 ...

  5. <转>SQL 左外连接,右外连接,全连接,内连接

    本文节选自:https://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html       连接条件可在FROM或WHERE子句中指 ...

  6. [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. javaWeb学习笔记——关于交叉连接-内连接-左外连接-右外连接的区别

    废话不说:直接上图1 图1-1 table1表 图1-2 table2 图1-3 cross join 交叉连接 图1-4 显示内连接 图1-5 左外链接 图1-6 右外链接 

  8. Hibernate迫切左外连接和迫切内连接

    •迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee  ...

  9. SQL连接:内连接、外连接、交叉连接。

    SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:             book表                                          stu表 1.内连接 ...

随机推荐

  1. [SVN Mac的SVN使用]

    在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...

  2. AngularJs ngCloak、ngController、ngInit、ngModel

    ngCloak ngCloak指令是为了防止Angular应用在启动加载的时候html模板将会被短暂性的展示.这个指令可以用来避免由HTML模板显示造成不良的闪烁效果. 格式: ng-cloak   ...

  3. Apache 优雅重启 Xampp开机自启 - 【环境变量】用DOS命令在任意目录下启动服务

    D:\xampp\apache\bin\httpd.exe" -k runservice Apache 优雅重启 :httpd -k graceful Xampp开机自启动  参考文献:ht ...

  4. BZOJ1047: [HAOI2007]理想的正方形

    传送门 蛤省省选果然水啊,我这种蒟蒻都能一遍A. 横向纵向维护两个单调队列,做两次求最大和最小的,总复杂度$O(NM)$ 码农题,考察代码实现能力 //BZOJ 1047 //by Cydiater ...

  5. 第3章 jQuery的DOM操作

    一.  DOM 分为DOM核心,HTML-DOM和CSS-DOM 1.DOM核心 不专属与javascript. 获取对象:document.getElementsByTagName('div') 获 ...

  6. Marshal

    https://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx http://tomosoft.jp/design/?p=4647 ht ...

  7. 获取<img src="sdf.jpg" Big="sf.jpg">中的big的值

    原代码: <img src="sdf.jpg" Big="sf.jpg" onclick="getsrc($(this).attr(" ...

  8. ViewController respondsToSelector:]: message sent to deallocated instance

    今天突然遇到这个问题,其实昨天下班的时候就已经有这个问题了, 就是先进入一个画页,然后再快速离开这个画页再进入其他画页就出现这个错误 了 找了好久也没有找出问题来,一开始以为是网络任务没有cancel ...

  9. from live writer

    <body> <div class="pagination"> <a href="#" class="first&quo ...

  10. php如何发起POST DELETE GET POST 请求

    关于POST,DELETE,GET,POST请求 get:是用来取得数据.其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制 post:是用来上传数据.要上传的数据放在request的he ...