数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
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条件查询的区别的更多相关文章
- 详解mysql数据库的左连接、右连接、内连接的区别
一般所说的左连接,外连接是指左外连接,右外连接.做个简单的测试你看吧. 先说左外连接和右外连接: SQL>select * from t1; ID NAME ---------- ------- ...
- SQL的几种连接:内连接、左联接、右连接、全连接、交叉连接
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
- Sql中的内连接,左连接以及右连接区别
转自:http://pangaoyuan.javaeye.com/blog/713177 有两个表A和表B. 表A结构如下: Aid:int:标识种子,主键,自增ID Aname:varchar 数据 ...
- SQL 左外连接,右外连接,全连接,内连接
原文地址 连接条件可在FROM或WHERE子句中指定,建议在FROM子句中指定连接条件.WHERE和HAVING子句也可以包含搜索条件,以进一步筛选连接条件所选的行. 连接可 ...
- <转>SQL 左外连接,右外连接,全连接,内连接
本文节选自:https://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html 连接条件可在FROM或WHERE子句中指 ...
- [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- javaWeb学习笔记——关于交叉连接-内连接-左外连接-右外连接的区别
废话不说:直接上图1 图1-1 table1表 图1-2 table2 图1-3 cross join 交叉连接 图1-4 显示内连接 图1-5 左外链接 图1-6 右外链接
- Hibernate迫切左外连接和迫切内连接
•迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee ...
- SQL连接:内连接、外连接、交叉连接。
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
随机推荐
- 三角形问题的解决复杂度O(n^3)和O(nlogn)的比较
问题描述: n条棍子组成一个三角形,使得三角形周少最大. 方法一: 暴力解则算法复杂度为O(n^3) #include<stdio.h> const int MAX_N=105 int m ...
- nginx跨域设置
nginx跨域问题例子:访问http://10.0.0.10/ 需要能实现跨域 操作:http://10.0.0.10/项目是部署在tomcat里面,tomcat跨域暂时还不会,按照网上的方法操作也没 ...
- chown -R命令的使用
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- win 2012 修改盘符
开始--运行 diskmgmt.msc ........
- Java排序算法——表插入排序
//================================================= // File Name : ListInsertSort_demo //----------- ...
- Java可变参数传递中可以接收多个对象
- hasClass addClass removeClass
//函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...
- dedecms标签的sql语句
{dede:sql sql='Select content from dede_arctype where id=1' titlelen='40′} [field:content/] {/dede:s ...
- 使用Topshelf 开发windows服务
在业务系统中,我们为了调度一些自动执行的任务或从队列中消费一些消息,所以基本上都会涉及到后台服务的开发.如果用windows service开发,非常不爽的一件事就是:调试相对麻烦,而且你还需要了解 ...
- python 多线程学习
多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...