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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:38:02+00:00 2026-05-31T09:38:02+00:00

My business partner and I are having issues selecting from a MySQL view that

  • 0

My business partner and I are having issues selecting from a MySQL view that has a HAVING clause.

The query simply selects a few fields from the view, determines a distance dynamically with a few calculations, and aliases it as ‘distance’ – then limits the results to those rows with a distance less than a supplied variable.

The distance is calculated using the Haversine formula, referenced by Google Maps:
https://developers.google.com/maps/articles/phpsqlsearch

Here is what I know:

1) When the HAVING clause is removed from the query, it returns all the results in the view successfully, including the calculated ‘distance’ for each row

2) When the HAVING clause is added to the query, it returns an empty result set

3) We also tried swapping the variable in the HAVING clause to a static number – this also returned an empty result set

The contents of the view seem irrelevant since everything works without the HAVING clause.

Here is the query:

SELECT 
restaurantName, 
restaurantID, 
locationID, 
locationCity, 
locationState, 
locationAddress, 
locationLatitude, 
locationLongitude,
( 3959 * acos( cos( radians('%s') ) * cos( radians( locationLatitude ) ) * cos( radians( locationLongitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( locationLatitude ) ) ) ) AS distance 
FROM newView 
HAVING distance < '%s' 
ORDER BY distance

Remember that the view calculates the ‘distance’ for every selected row perfectly without the HAVING clause, so we are convinced the problem lies there… when we take it out, everything works but every row in the view is returned.

Any ideas why the HAVING clause is returning an empty set? Is the HAVING clause not compatible with views?

  • 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-31T09:38:04+00:00Added an answer on May 31, 2026 at 9:38 am

    Try this:

    select * from (
        SELECT 
        restaurantName, 
        restaurantID, 
        locationID, 
        locationCity, 
        locationState, 
        locationAddress, 
        locationLatitude, 
        locationLongitude,
        ( 3959 * acos( cos( radians('%s') ) * cos( radians( locationLatitude ) ) * cos( radians( locationLongitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( locationLatitude ) ) ) ) AS distance 
        FROM newView
    ) S
    where distance < '%s' 
    ORDER BY distance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My company has recently became a Microsoft Certified Partner and from a business perspective
A business partner has suggested building a web services library with JSON-RPC rather that
Is business intelligence a buzzword that has no real meaning to software developers, or
I have some code from a Chinese business partner, and all of the comments
We have a business partner that is IFRAMEing some data to one of our
I am designing a new XSD to capture points information from a business partner.
Original question: An affiliate partner of us has a website that is vulnerable to
My business has a .NET web service (not WCF) hosted in IIS on a
I am in biz with one partner and he set the business up with
The business context of the use case is that we are building a backend

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.