0

I am trying to count of coupons sold by each store from the list of stores within 20 miles range. I know the following syntax will work if there is only 1 store.

SELECT sum(couponscount) as count where restaurant IN (SELECT storename where bhal bhal bhal and output is one value)

What is I the IN (SELECTstorenamewhere bhal bhal bhal and output is multiple values) will return multiple values?

Like in my case the complete SQL is like and its not working

SELECT sum(couponscount) as count FROM `coupons` having `restaurant` IN (SELECT `storename`, ((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) AS `distance` FROM `stores` WHERE `status`=’active’ HAVING `distance` <=20)

Is there anyway to make it working?

5
  • How is it not working? What is the problem? Commented Sep 9, 2014 at 14:54
  • 1
    You need to move the distance condition from the SELECT to WHERE. Commented Sep 9, 2014 at 14:55
  • As a rule of thumb: When you see HAVING without GROUP BY there is usually something wrong with the query. Commented Sep 9, 2014 at 15:05
  • @ThorstenKettner Not in mysql... Commented Sep 9, 2014 at 15:06
  • 1
    @Mihai: Yes, also in MySQL. It usually shows that the author of the query doesn't exactly know what he/she is doing :-) Commented Sep 9, 2014 at 15:09

2 Answers 2

1
SELECT sum(couponscount) AS COUNT,restaurant
FROM `coupons`
WHERE `restaurant` IN
    (SELECT `storename`
     FROM `stores`
     WHERE `status`='active'           
       AND
      ((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI()) *60 * 1.1515) <=20)
GROUP BY restaurant

Also use proper quotes for active.

Sign up to request clarification or add additional context in comments.

1 Comment

Still didn't work even after your edited the answer. Error Unknown column 'latitude' in 'having clause' but that latitude is used column in the table but there is something wrong in your syntax.
1

Presumably, you want to get the count of coupons from stores within a distance of 20. Moving the having condition to a where clause should do what you want:

SELECT sum(couponscount) as count
FROM `coupons`
WHERE `restaurant` IN (SELECT `storename`
                       FROM `stores`
                       WHERE `status` = 'active' AND
                              ((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) <= 20
                      );

You had a major syntax problem because your subquery returned two columns. When you use a subquery with in, you can only return one column, in this case, storename. I moved the code for the distance calculation to the where clause. No having clause is needed either in the subquery or the outer query.

1 Comment

Lol, that returned complete count of all stores but I need count per each store so that I can use a while loop in php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.