問:
SELECT
a.channel,
b.type proOperateType,
c.type catOperateType,
a.shop_code shopCode,
a.status syncStatus,
a.type errorType,
a.name,
a.code,
a.reason
FROM fresh_menu_error_msg a
LEFT JOIN fresh_product_sync b on a.request_id = b.request_id
LEFT JOIN fresh_category_sync c on a.request_id = c.request_id
where a.company_id = '2399'
order by a.create_date desc
大佬 這個sql a b c三張表關聯(lián)庐杨,,但是一個關聯(lián)條件requestId只能同時存在 a b ,或者 a c兩張表里艰争,,這里用inner join查出來的100%是空的桂对,甩卓,所以還有什么別的優(yōu)化方法么?
答:
SELECT a.*,b.`type` FROM fresh_menu_error_msg a
INNER JOIN fresh_product_sync b ON a.request_id = b.request_id
WHERE a.`company_id` = '2399'
UNION ALL
SELECT a.*,c.`type` FROM fresh_menu_error_msg a
INNER JOIN fresh_category_sync c ON a.request_id = c.request_id
WHERE a.`company_id` = '2399'
哦 忘了 a.company_id蕉斜, b.request_id 逾柿,c.request_id 都加個索引