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', 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='运营分类和商品挂载关系表';