PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] How can I remove subquery?

 October 29, 2022     group-by, left-join, on-clause, sql, subquery     No comments   

Issue

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) 

Solution

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)


Answered By - forpas
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing