sam sam - 6 months ago 9
PHP Question

How to use or condition in group by having clause

How we can use OR condition in group by clause below is my sql query

SELECT count(id) as num_rows,
(3959 * acos ( cos ( radians(41.766908)) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-81.1526976)) + sin ( radians(41.766908) ) * sin( radians( lat ) ))) AS distance
FROM (`items`)
WHERE `status` = 1
GROUP BY `distance`
HAVING `distance` <=50


In this query I have to add OR condition as well with group by clause that is

(GROUP BY `distance` HAVING `distance` <=50 OR `nationwide`=1)


How we can execute above query ??

Below is my result - and I have to add OR condition in having by clause

id nationwide distance

1 2 22.4347083649522

1 2 46.8127635462102

8 2 10.0201103523007

13 1 11.888890536392

Answer

Probably it will help you

GROUP BY `distance`(HAVING `distance` <= 50 OR `nationwide` = 1);