数据库左连接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条件都可以完成,但反之则不是啦。

建表语句:

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

采用 inner join 过滤 左表

 1 SELECT 
 2     t1.relation_id,
 3     t1.product_id,
 4     t1.product_code,
 5     t2.product_id AS p_product_id,
 6     t2.product_name AS p_product_name,
 7     t2.product_code AS p_product_code,
 8 FROM
 9     t_salecategory_product_relation t1
10         JOIN
11     t_product t2 ON t1.product_id = t2.product_id  and t1.category_id = 1

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

 1 SELECT 
 2     t1.relation_id,
 3     t1.product_id,
 4     t1.product_code,
 5     t2.product_id AS p_product_id,
 6     t2.product_name AS p_product_name,
 7     t2.product_code AS p_product_code,
 8 FROM
 9     t_salecategory_product_relation t1
10         LEFT JOIN
11     t_product t2 ON t1.product_id = t2.product_id
12 WHERE
13     t1.category_id = 1;

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

 1 SELECT 
 2     t1.relation_id,
 3     t1.product_id,
 4     t1.product_code,
 5     t2.product_id AS p_product_id,
 6     t2.product_name AS p_product_name,
 7     t2.product_code AS p_product_code,
 8 FROM
 9     t_salecategory_product_relation t1
10         LEFT JOIN
11     t_product t2 ON t1.product_id = t2.product_id  and t1.category_id = 1 
原文地址:https://www.cnblogs.com/xunux/p/4432770.html