tags:

views:

98

answers:

4

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'
A: 

A debugging question for you, what happens if you have a simple Select where you Select e.date1 from 'events' as 'e' ? I usually pull back to that type of query in case for some reason my syntax error is being hidden by something.

By the way, I noticed that you have 'e.date1' and then use e.date1, i.e. no single quotes. That would seem to not work, though I am not a MySQL guy.

Driss Zouak
A: 

edit: the error message is »Unknown column 'date1' in 'where clause'«

the reason for this is, that the op aliases the if statement to the alias-name 'e.date1'. in his where clause he writes e.date1 without any backticks. e.date1 will look for a column date1 in table e, and is different from the alias 'e.date1' (with backticks)


you are aliasing the subquery to the name "e.date1" and then reference the column "date1" in the table "e"—which does not exist.

you have to either alias to "date1" and then use WHERE date1 or write the where as WHERE ``e.date1`` (fucked up by markdown …)

full code:

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'

or

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'
knittl
Won't work, see this answer: http://stackoverflow.com/questions/2586750/mysql-column-not-found/2586807#2586807
Felix Kling
have you actually tried it? he's aliasing to a different name than he uses in his where clause ("e.date1" vs. "e"."date1")
knittl
@knittl: Read the documentation: http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
Felix Kling
nevertheless he creates a column alias and uses another …
knittl
@knittl: I don't get your point.
Felix Kling
@felix: he creates the alias as 'e.date1' (a single name) and in the where he uses e.date1 (column date1 in table e) THAT'S THE REASON WHY THE ERROR IS **CANNOT FIND COLUMN DATE1** (in table e)
knittl
This is only part of the the answer. You are right you cannot alias as column this way. But even your code won't work as you cannot use aliases in the `WHERE` clause.
Felix Kling
beat me to it -.-
knittl
+3  A: 

You cannot alias a column with <tablename>.<name>. Instead of AS e.date1 you really have to use AS 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 WHERE clauses.

From the documentation:

An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column

and

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

But you can use aliases in a HAVING clause:

SELECT
  IF( e.weekly,
    DATE_ADD(DATE(e.time),
    INTERVAL CEIL(DATEDIFF('2010-04-08', e.time)/7) WEEK ),
    DATE(e.time)) AS `date1`,
  `v`.`lat`,
  `v`.`lng` 
FROM `events` AS `e`
INNER JOIN `venues` AS `v` ON e.venue_id = v.id
HAVING date1 > '2010-09-01'

or you repeat the whole IF statement in the where clause.

Felix Kling
A: 

Believe it or not, it appears you need to add the same IF statement in the where clause.

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 IF( e.weekly,
      DATE_ADD(DATE(e.time),
      INTERVAL CEIL(DATEDIFF('2010-04-08', e.time)/7) WEEK ),
   DATE(e.time)) > '2010-09-01'
Paul Tomblin