MySQL error in where clause : Unknown column

MySQLselect

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

Unknown column 'have_discount' in 'where clause'

My query is:

SELECT
    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?

Best Answer

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