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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:41:01+00:00 2026-05-23T08:41:01+00:00

I have an issue where I need to AS a column in a MySQL

  • 0

I have an issue where I need to AS a column in a MySQL query and execute a BETWEEN on that column. The simplest version I can come up with that illustrates the problem is this:

SELECT ID AS post_id FROM wp_posts WHERE (post_id BETWEEN 10 AND 20)

This throws the following error:

#1054 - Unknown column 'post_id' in 'where clause'

Is there a way to make the newly created post_id column visible to the BETWEEN operator?

UPDATE:

This is my actual query. As you can see, it is slightly more complicated:

SELECT wp_postmeta.post_id,
MAX(CASE WHEN wp_postmeta.meta_key='store_lng' THEN wp_postmeta.meta_value END ) AS lng,
MAX(CASE WHEN wp_postmeta.meta_key='store_lat' THEN wp_postmeta.meta_value END ) AS lat
FROM wp_postmeta
LEFT JOIN wp_posts ON (wp_posts.ID=wp_postmeta.post_id) 
WHERE
(lng BETWEEN 150.29793837662 AND 152.1161201948)
AND 
(lat BETWEEN -34.775666623377 AND -32.957484805195) 
AND
wp_posts.post_status='publish'
AND
wp_posts.post_type='store'
GROUP BY wp_postmeta.post_id
  • 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-23T08:41:01+00:00Added an answer on May 23, 2026 at 8:41 am

    From the MySQL documentation:

    It is not permissible to refer to a
    column alias in a WHERE clause,
    because the column value might not yet
    be determined when the WHERE clause is
    executed.

    You’ll either have to use ID instead of post_id in the WHERE clause or use a HAVING clause.

    For example:

    SELECT ID AS post_id FROM wp_posts WHERE ID BETWEEN 10 AND 20
    

    Update: Based on your most recent update, you could use a subquery as already suggested or as I suggested earlier, you could use a HAVING clause.

    For example:

    SELECT    wp_postmeta.post_id,
              MAX(CASE WHEN wp_postmeta.meta_key = 'store_lng' THEN wp_postmeta.meta_value END) AS lng,
              MAX(CASE WHEN wp_postmeta.meta_key = 'store_lat' THEN wp_postmeta.meta_value END) AS lat
    FROM      wp_postmeta
    LEFT JOIN wp_posts
    ON        wp_posts.ID = wp_postmeta.post_id
    WHERE     wp_posts.post_status = 'publish' AND
              wp_posts.post_type = 'store'
    GROUP BY  wp_postmeta.post_id
    HAVING    lng BETWEEN 150.29793837662 AND 152.1161201948 AND 
              lat BETWEEN -34.775666623377 AND -32.957484805195
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with a my SQL query. I need to execute a
We have an issue where we need to modify keys for users that are
So here's my issue. I have an image that I need to shrink. The
I rarely use SQL and have a unique issue I need to solve. I
I have searched for this but nobody have the same issue.. Now I need
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have a table that has a column which holds the id of a
I am really struggling with the issue below and need some help...I have found
I am writing a SQL query that I would like to add a column
I have an xml column in my SQL Server database that has records in

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.