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='運營分類和商品掛載關系表';

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='商品基本屬性表';
采用 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