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

  • SEARCH
  • Home
  • 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 8973137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:22:45+00:00 2026-06-15T18:22:45+00:00

I have these two different queries. This query pulls the records from posts table

  • 0

I have these two different queries.

This query pulls the records from “posts” table as per their replies counter. Only posts with replies are returned with this query:

SELECT posts.title, posts.num, posts.status, COUNT( posts_replies.post_num)  AS count
FROM posts_replies
INNER JOIN posts ON ( posts_replies.post_num = posts.num )
WHERE posts.status = 1
AND posts.category='uncategorized'
GROUP BY posts.num

And this is a new query that i want to merge with the above one to pull and sort records as per gps.

SELECT num, title, ( 3959 * acos( cos( radians( 37 ) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians( -122 ) ) + sin( radians( 37 ) ) * sin( radians( lat ) ) ) ) AS distance
FROM posts
HAVING distance <75
ORDER BY distance

This query uses the columns lat and long to return records that are within the 75 miles radius of the user.

I am not a sql expert and don’t know how to merge both of the queries to gather results having the following criteria:

  1. Only return posts with replies
  2. Sort by their distance
  3. Sort by their number of replies

Any help would be highly appreciated.

Thanks!

  • 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-15T18:22:46+00:00Added an answer on June 15, 2026 at 6:22 pm

    The having clause in the second query does not look correct. In most dialects of SQL is would not be allowed without a group by. I forget if MySQL t implicitly treats the whole query as an aggregation (returning one row) or if the having gets converted to a where. In either case, you should be explicit and use where when there are no aggregations.

    You can just combine them by putting in the where clause. I would do it with a subquery, to make the variable definitions clearer:

          SELECT p.title, p.num, p.status, p.distance,
                 COUNT( p_replies.post_num)  AS count
          FROM posts_replies pr  INNER JOIN
               (select p.*,
                       ( 3959 * acos( cos( radians( 37 ) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians( -122 ) ) + sin( radians( 37 ) ) * sin( radians( lat ) ) ) ) AS distance
                from posts p
               ) p
               ON pr.post_num = p.num
          WHERE p.status = 1 AND
                p.category='uncategorized' and
                distance < 75
          GROUP BY p.num
          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 a page that pulls together aggregate data from two different tables. I
So these two methods have the same signature but different constraints public static void
In my application, I have two queries that occur from time to time (from
i have these two queries, that i want to join together, but am not
I'm trying to determine the relative performance of two different queries and have two
Lets say there are two pages with two different html elements which have the
I have following two arrays. I want the difference between these two arrays. That
I have these two lines in my web.xml <url-pattern>/</url-pattern> : Index Servlet and <url-pattern>/login</url-pattern>
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
I have these two modules : package G1; sub new { my $class =

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.