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 7017899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:58:26+00:00 2026-05-27T22:58:26+00:00

I have a database full of users that roughly looks like this: | id

  • 0

I have a database full of users that roughly looks like this:

| id | username | birthdate  | sex | zip_code | latitude  | longitude  |
|----+----------+------------+-----+----------|-----------+------------|
|  1 |  userA   | 1986-04-05 |  M  |  90210   | 34.088808 | -118.40612 |
|  2 |  userB   | 1984-08-15 |  F  |  10011   | 40.741012 | -74.00012  |
|  3 |  userC   | 1984-11-25 |  F  |  10014   | 40.734718 | -74.00532  |

I have searched the internet and found 2 very helpful queries that allow me to calculate the distance and age of a particular user. Here is the distance query I have come up with:

SELECT username, (
(
ACOS( SIN( $latitude * PI( ) /180 ) * SIN( latitude * PI( ) /180 ) + COS( $latitude * PI( ) /180 ) * COS( latitude * PI( ) /180 ) * COS( (
$longitude - longitude
) * PI( ) /180 ) ) *180 / PI( )
) *60 * 1.1515
) AS  `distance` 
FROM  `userList` 
HAVING  `distance` <=15
ORDER BY  `distance` ASC 
LIMIT 0 , 30

This gives me a result of all the users in my database that is located 15 miles or less from me. Here is the age query:

    SELECT username, YEAR( CURDATE( ) ) - YEAR( birthdate ) - IF( STR_TO_DATE( CONCAT( YEAR( CURDATE( ) ) ,  '-', MONTH( birthdate ) ,  '-', IF( MONTH( birthdate ) =2
AND DAY( birthdate ) =29, DAY( DATE_ADD( CONCAT( YEAR( CURDATE( ) ) ,  '-03-01' ) , INTERVAL -1
DAY ) ) , DAY( birthdate ) ) ) ,  '%Y-%c-%e' ) > CURDATE( ) , 1, 0 ) AS age
FROM userList
LIMIT 0 , 30

What I would like to do is combine these queries in such a way where a user can search for other users that are located less than X miles away AND older than Y but younger then Z

How can I go about combining these queries into one? I’m not very well traversed in SQL queries and don’t know which way to proceed.

  • 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-05-27T22:58:27+00:00Added an answer on May 27, 2026 at 10:58 pm
    Select 
      username,
      (age_stuff) as age,
      (distance_stuff) as distance
    from userList
    where 
      distance < 15
      and age < 24 and age > 64
    order by distance
    limit 30 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database full of text fields that look like this: (paragraph of
I have a MySQL database full of user information, like their username, password, email,
I have a database full of customer data. It's so big that it's really
I have a database that has a users first and last name. Some cases
I have three tables in my sample database: Users ID Username Password Roles ID
I have a couple of table on my database that specify witch users (
I have a full multi-tenant database with TenantID's on all the tenanted databases. This
I have three database tables: users , user_profiles and friends : users id username
I have a database full of small HTML documents and I need to programmatically
I have a database full of two-dimensional data - points on a map. Each

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.