Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I try to execute the query below, I receive the error message:

Unknown column 'have_discount' in 'where clause'

My query is:

    DISTINCT(MP.item_id), item_title, item_user_id, item_price, item_discount_price, url_slug, 
    thumbnail_img, thumbnail_ext, thumbnail_width, thumbnail_height, is_free_item, default_img, 
    default_img_ext, default_img_width, default_img_height, item_category_url_str, 

    IF( ( DATE( NOW() ) BETWEEN item_discount_fromdate AND item_discount_todate), 1, 0 ) AS have_discount,

    item_category_id, NOW() as date_current, date_activated, MP.total_downloads, 
    MP.item_sold, MP.total_views, MP.total_comments, MP.total_likes AS likes, 
    MP.total_dislikes, MP.total_rating, MP.rating_count, MP.total_users_rated, 
    (MP.total_rating/MP.rating_count) as rating, MP.default_img_ext , MPL.item_title, 
    MPL.lang_id, MPL.item_description, MPL.item_highlight_text, MP.user_section_id, is_downloadable_item 

FROM mp_item AS MP, mp_item_lang AS MPL

WHERE have_discount = 1
AND item_status = 'Ok'
AND MP.item_id = MPL.item_id
AND item_user_id = 3
AND MPL.lang_id = 2
ORDER BY date_activated DESC
LIMIT 0,12

I am trying to list items with a discount. How can I do this?

share|improve this question
Also note that DISTINCT is not a function. It applies to the whole rows of the result, not to one column only (MP.item_id) as you may think. – ypercubeᵀᴹ Nov 27 '12 at 22:58
up vote 4 down vote accepted

Now, if you rewrite the query in a clear manner, you can see where's the troublesome column. From what I know of general SQL you can't use the alias of the column in the WHERE clause (but you can use it in the ORDER BY clause).

So you'd have to rewrite the query something like:

Select *
from (
    -- Your nice query without the have_discount filter
) as Aux
WHERE Aux.have_discount = 1
ORDER BY date_activated DESC
LIMIT 0,12
share|improve this answer
Thanks Rolando, missed that one :). – Marian Nov 27 '12 at 22:41
Thank you Marian, it works perfectly. – Murat Nov 28 '12 at 15:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.