如何使用内连接优化mysql查询

时间:2017-01-24 09:51:43

标签: mysql sql database join clause

select id from customer_details where store_client_id = 2 
And
    id  NOT IN (select customer_detail_id from  orders 
        where store_client_id = 2 and total_spent > 100 GROUP BY customer_detail_id ) 
Or
    id IN (select tcd.id from property_details as pd, customer_details as tcd 
        where pd.store_client_id = 2 and pd.customer_detail_id = tcd.customer_id and pd.property_key = 'Accepts Marketing' 
        and pd.property_value = 'no') 
And 
    id IN (select customer_detail_id from orders 
        where store_client_id = 2 GROUP BY customer_detail_id HAVING count(customer_detail_id) > 0 ) 
Or
    id IN (select tor.customer_detail_id from ordered_products as top, orders as tor 
        where tor.id = top.order_id and tor.store_client_id = 2 
        GROUP BY tor.customer_detail_id having sum(top.price) = 1)`

我有内部连接的mysql查询,所以当它在mysql服务器上运行时,它会减慢无法找到的问题。
但在4-5分钟后,它将返回15000条记录。这个记录可能不是问题。
在一些教程中建议使用Inner join,Left join,... 但我不知道如何在Join子句中转换此查询。 任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)