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. git命令笔记

    git -bare init git remote add origin ssh://myserver.com/home/git/myapp.git git remote show origin [r ...

  2. PHP之:PHP编程效率的20个要点

    [导读] 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数” 用单引号 ...

  3. ASP.NET WEB API 测试

    编码时测试: Postman + Fiddler4 Postman进行发包 Fiddler4进行抓包 编码结束后测试: 通过Nuget引入组件WebApiTestClient: 接口文档 类属性 测试

  4. 第1个linux驱动___打印"hello world"

    为了方便后续的深入,我们在驱动程序中用printk( )函数来打印"hello world",printk( )是内核中自带的函数,专门用于在打印内核信息.在安装驱动模块到内核中的 ...

  5. OpenGLES入门笔记四

    原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...

  6. iOS qrcode 默认尺寸与修改

    四种容错格式的尺寸:27.31.31.35. // 5.将CIImage转换成UIImage,并放大显示 UIImage *imagex = [UIImage imageWithCIImage:out ...

  7. easyUI validate函数【总结篇-部分转】

    以下是自己总结和修改别人的帖子和资源整理出来的一些常用验证函数,备用,交流. <body>邮箱验证:<input type="text" validtype=&q ...

  8. DIV CSS 网页兼容全搞定 (IE6 IE7 IE8 IE9 火狐 谷歌)

    CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middl ...

  9. OC-点语法

    点语法的本质:方法调用 #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...

  10. Orchard源码分析(5):Host相关(Orchard.Environment.DefaultOrchardHost类)

    概述 Host 是应用程序域级的单例,代表了Orchard应用程序.其处理应用程序生命周期中的初始化.BeginRequest事件.EndRequest事件等. 可以简单理解为HttpApplicat ...