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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:15:09+00:00 2026-05-29T09:15:09+00:00

I am encountering some unexpected behaviour with a MySQL select query. I am running

  • 0

I am encountering some unexpected behaviour with a MySQL select query. I am running the query:

SELECT `refno`, `subdomain`, `toplevels`, `renew_until`, `expiry_date`, 
(YEAR(`renew_until`) - YEAR(`expiry_date`)) AS `renew_for` FROM `testing_names` 
WHERE `expiry_date` >= DATE(NOW()) AND `renew_for` >= 0

which returns (as expected):

|  refno  |  subdomain  |  toplevels  |  renew_until  |  expiry_date  |  renew_for  |
|-----------------------------------------------------------------------------------|
|  5      |  domain1    |  com        |  2014-02-02   |  2014-02-02   |  0          |
|  45     |  domain2    |  net        |  2014-01-27   |  2013-01-27   |  1          |

However the following query (note the different comparison on renew_for) returns an empty set:

SELECT `refno`, `subdomain`, `toplevels`, `renew_until`, `expiry_date`, 
(YEAR(`renew_until`) - YEAR(`expiry_date`)) AS `renew_for` FROM `testing_names` 
WHERE `expiry_date` >= DATE(NOW()) AND `renew_for` > 0

In this scenario, I was expecting row #45; what is wrong with my query? Am I utilising renew_for in the correct way?

  • 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-29T09:15:10+00:00Added an answer on May 29, 2026 at 9:15 am

    Other RDBMS won’t allow this syntax: column aliases should not be available in the WHERE clause.
    MySQL does it’s own thing though with erratic results:

    Standard SQL doesn’t allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.

    So, change the WHERE condition to this

    WHERE `expiry_date` >= DATE(NOW()) AND (YEAR(`renew_until`) - YEAR(`expiry_date`)) > 0
    

    Or use a derived table

    SELECT *
    FROM
       (
        SELECT 
            `refno`, `subdomain`, `toplevels`, `renew_until`, `expiry_date`, 
             (YEAR(`renew_until`) - YEAR(`expiry_date`)) AS `renew_for`
        FROM `testing_names` 
        ) T
    WHERE `expiry_date` >= DATE(NOW()) AND `renew_for` > 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm encountering some peculiarities with LINQ to SQL. With a relatively simple query, I
I am encountering some very strange behaviour with a Flex 4.1 app I am
So I am seemingly encountering some strange behavior when using NSString 's -sizeWithFont family
I am creating reports using the Microsoft ReportViewer control. I am encountering some formatting
I'm writing some mail-processing software in Python that is encountering strange bytes in header
I've been encountering some strange behavior when trying to find a key inside a
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I'm encountering this error while compiling some old VC++ 6.0 source code. error C2632:
We have some users which are using lower-CPU powered machines and they're encountering slow
This site was working properly before I started encountering an error after entering some

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.