Jump to content

Need help with complicated query


rhughessa

Recommended Posts

So here's the query I'm trying which is returning a "error in your syntax" error

 

SELECT COUNT(restaurant_id) AS number, category, lat, lon, ( 3959 * acos( cos( radians(41.044128) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-81.524117) ) + sin( radians(41.044128) ) * sin( radians( lat ) ) ) ) AS distance FROM restaurants HAVING distance < 25 GROUP BY category ORDER BY category

 

This is an attempt at combining the following 2 queries that both work independently.

 

1.

SELECT COUNT(restaurant_id) AS number FROM restaurants GROUP BY category ORDER BY category

 

 

2.

SELECT lat, lon, ( 3959 * acos( cos( radians(41.044128) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-81.524117) ) + sin( radians(41.044128) ) * sin( radians( lat ) ) ) ) AS distance FROM restaurants HAVING distance < 25

 

 

Thanks in advance,

 

Robert

Link to comment
https://forums.phpfreaks.com/topic/231900-need-help-with-complicated-query/
Share on other sites

Hi

 

The problem is that you have the HAVING clause in the wrong place. If should be after the GROUP BY.

 

However you shouldn't have non aggregate columns in the SELECT clause which are not in the GROUP BY clause. MySQL is pretty tolerant of this, but some flavours of SQL will reject it out of hand.

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.