The SQL query without where statement runs great and outputs good results, but when I include WHERE condition it shows Unknown column ‘date1’ in ‘where clause’. What’s the problem?
SELECT
IF( e.weekly,
DATE_ADD(DATE(e.time),
INTERVAL CEIL(DATEDIFF('2010-04-08', e.time)/7) WEEK ),
DATE(e.time)) AS `e.date1`,
`v`.`lat`,
`v`.`lng`
FROM `events` AS `e`
INNER JOIN `venues` AS `v` ON e.venue_id = v.id
WHERE e.date1 > '2010-09-01'
You cannot alias a column with
<tablename>.<name>. Instead ofAS e.date1you really have to useAS date1.(If you omit the backticks to create the alias you will get an SQL syntax error.)
But this is only one reason. The other is that aliases can’t be used in
WHEREclauses.From the documentation:
and
But you can use aliases in a
HAVINGclause:or you repeat the whole
IFstatement in the where clause.