I need to remove the subquery. But I must maintain the condition. What can I do?
(SELECT * FROM `customer_orders` where status=3)
SELECT cus.id,cus.customer_name,cus.mobile,cus.email,
COUNT(CAST(cus_ord.customer_id AS INT)) AS Total_Order ,SUM(cus_ord.order_total_amt) AS Total_Amount
FROM customers as cus
LEFT JOIN (SELECT * FROM `customer_orders` where status=3) as cus_ord on CAST(cus_ord.customer_id AS INT)= CAST(cus.id AS INT)
GROUP BY CAST(cus.id AS INT)
CodePudding user response:
You can move the condition in the ON
clause:
......................
FROM customers AS cus LEFT JOIN customer_orders AS cus_ord
ON CAST(cus_ord.customer_id AS INT) = CAST(cus.id AS INT) AND cus_ord.status = 3
......................
Also:
COUNT(CAST(cus_ord.customer_id AS INT))
is equivalent to just:
COUNT(cus_ord.customer_id)