数据库左连接leftjoin、右连接rightjoin、内连接innerjoinon及wh。。。
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 '挂载类型:\r\n                1:⾃动挂载;\r\n                2:⼿动挂载\r\n            ',
9  `opt_type` smallint(6) DEFAULT NULL COMMENT '操作类型: 1 更新  2 删除 ,默认为1',sql left join 多表连接
10  `mount_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURREN
T_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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。