MySQL query goes wrong

Steynsk
Steynsk used Ask the Experts™
on
Hi Experts,

I've got this query:

SELECT id, date(date_format(datum, '%Y-%m-%d')) as dag FROM programmering

It works excelently but when I try to add a condition:

SELECT id, date(date_format(datum, '%Y-%m-%d')) as dag FROM programmering WHERE dag = '2012-05-26'

I get this error:

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

strange because the row dag exists in the first version of this query but I can't use it in a condition?

All help is appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
You cannot use an alias of a selected column in your WHERE clause.  Also, is datum of datetime datatype?  Then you should just use:

SELECT id, date(datum) as dag FROM programmering WHERE date(datum)= '2012-05-26'

Author

Commented:
Thank you johanntagle

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial