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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.