Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:54:56+00:00 2026-06-03T07:54:56+00:00

I have the below query where I join two tables, the query is working

  • 0

I have the below query where I join two tables, the query is working when I remove the group by at the end, but it returns too many rows for me.

The first priority is to have the order by distance (which is calculated within the query), subsequently I want only the unique tr_id with the smallest distance.

When I use group by I get the following error:

1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near GROUP BY tr_id LIMIT 0, 30 at line 2.

SELECT op.operatorid AS tr_id, de.latitude AS
tolatitude, de.longitude AS tolongitude, de.surname AS tosurname,
de.firstname AS tofirstname, de.did AS to_id,
de.tzz AS tmzz, 
op.bkk AS tobkk, 
( 3959 * acos( cos( radians(52.524368165134284) ) * cos( radians(
de.latitude )
 ) * cos( radians( de.longitude) - radians(13.410530090332031) ) + sin(
radians(52.524368165134284) ) * sin( radians( de.latitude ) ) ) ) 
AS distance 
FROM de 
INNER JOIN op ON de.did = op.did 
WHERE de.type='Dutch' AND de.longitude > 11.6118868785
AND de.longitude < 15.2091733022 AND de.latitude > 50.7257249533
AND de.latitude < 54.323011377 having distance < 200 
GROUP BY tr_id
ORDER BY distance
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:54:58+00:00Added an answer on June 3, 2026 at 7:54 am

    HAVING clause must come after any GROUP BY clause,so your query must be:-

    SELECT op.operatorid AS tr_id, de.latitude AS
    tolatitude, de.longitude AS tolongitude, de.surname AS tosurname,
    de.firstname AS tofirstname, de.did AS to_id,
    de.tzz AS tmzz, 
    op.bkk AS tobkk, 
    ( 3959 * acos( cos( radians(52.524368165134284) ) * cos( radians(
    de.latitude )
    ) * cos( radians( de.longitude) - radians(13.410530090332031) ) + sin(
    radians(52.524368165134284) ) * sin( radians( de.latitude ) ) ) ) 
    AS distance 
    FROM de 
    INNER JOIN op ON de.did = op.did 
    WHERE de.type='Dutch' AND de.longitude > 11.6118868785
    AND de.longitude < 15.2091733022 AND de.latitude > 50.7257249533
    AND de.latitude < 54.323011377  
    GROUP BY tr_id having distance < 200
    ORDER BY distance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I am trying to join in a query, but
I have two tables with some ID's and I need a query that returns
I have these two tables setup and the below query running. The problem is
I'm trying to join two relatively simple tables together, but my query is experiencing
We have the query below. Using a LEFT OUTER join takes 9 seconds to
I'm using Sql-Server, and I have the below query which returns all columns that
I have below a working query that needs to be simplified. The reason is
I have the query below that when run it says that 325 rows were
I am using Access 2007 to create an SQL query to join two tables.
I have two tables as below: Student: oneID (primary key), col1, col2, col3, subId,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.