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. CTO、技术总监、首席架构师的区别

    2016年11月30日13:22:26[转] CTO.技术总监.首席架构师的区别 提升自已的能力,比如专业技术,行业发展趋势,技术发展趋势,协调能力,组织能力,管理能力等[技术总监] 需要从技术总监和 ...

  2. HTML5学习总结-04 音频&视频播放

    一 音频播放 1 Audio(音频) HTML5提供了播放音频文件的标准 2 control(控制器) control属性攻添加播放,暂停和音量空间. 3 标签定义声音 <audio> 例 ...

  3. 捉襟见肘之UIScrollView 【一】

    参考地址:http://segmentfault.com/a/1190000002412930 另一个优秀的UIScrollView实践文章地址:http://tech.glowing.com/cn/ ...

  4. MongoDb学习1

    目标框架必须是 4.5以上,最新MongoDb.Driver 是 2.2.4(与MongoDb.Driver 的1.x版本差别较大) 官方文档 http://mongodb.github.io/mon ...

  5. WinForm------BarManager中各种属性设置

    1.offset:红色Tool距离左边Tool的偏移量

  6. UIView UIwindow

    UI:用户界面,用户能看到的各种各样的页面元素 UIview :代表屏幕上的一个矩形区域,管理界面上的内容       创建UIview 1.开辟空间并初始化视图(初始化时,给出视图位置和大小 2.对 ...

  7. Nginx+HTTPS(SSL/TLS)

    环境 首先确保机器上安装了openssl和openssl-devel rpm -qa | grep openssl #yum install openssl #yum install openssl- ...

  8. bash: ifconfig: command not found解决方法

    1.问题: #ifconfig bash: ifconfig: command not found 2.原因:非root用户的path中没有/sbin/ifconfig ,其它的命令也可以出现这种情况 ...

  9. json_decode

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  10. MVC下分页的自定义分页一种实现

    1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...