问:
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三张表关联,,但是一个关联条件requestId只能同时存在 a b ,或者 a c两张表里,,这里用inner join查出来的100%是空的,,所以还有什么别的优化方法么?
答:
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 都加个索引