Avatar of c0mrad
c0mrad
 asked on

MySQL Sub-Query

I am trying to re-write this query without the subquery. It has been a while since I had to rewrite a query and was wondering if anyone may be able to think of a better way to write this:

SELECT `order_groups`.*
FROM `order_groups`  
WHERE `order_groups`.`ship_notified` = 0
AND `order_groups`.`affiliate_id` = 51
AND (
            (
                  select count(*)
                  from orders
                  where order_group_id=order_groups.id
                  and state != 'shipped'
                  and state != 'canceled'
            ) = 0
      AND (
                  select count(*)
                  from orders
                  where order_group_id=order_groups.id
                  and state = 'shipped'
            )
> 0);
MySQL Server

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sharath S

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
c0mrad

ASKER
Tried to run, but I get a bunch of errors.
Sharath S

What are the errors?
Sharath S

check this
SELECT `order_groups`.* 
  FROM `order_groups` 
       INNER JOIN (SELECT SUM(CASE 
                          WHEN state NOT IN ( 'shipped', 'canceled' ) THEN 1 
                          ELSE 0 
                        end) AS Cnt1, 
                    SUM(CASE 
                          WHEN state IN ( 'shipped' ) THEN 1 
                          ELSE 0 
                        end) AS Cnt2 
               FROM orders) AS t1 
         ON t1.order_group_id = order_groups.id 
 WHERE `order_groups`.`ship_notified` = 0 
   AND `order_groups`.`affiliate_id` = 51 
   AND t1.Cnt1 = 0 
   AND t1.Cnt2 > 0 

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
PortletPaul

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Sharath S

Yes, I missed that. Thanks PortletPaul for correcting that.
PortletPaul

np. wish I could type seelct without having to correct it once or twice :)