Using column alias in WHERE clause of MySQL query produces an error

后端 未结 8 1331
北荒
北荒 2020-11-22 03:40

The query I\'m running is as follows, however I\'m getting this error:

#1054 - Unknown column \'guaranteed_postcode\' in \'IN/ALL/ANY subquery\'

<
8条回答
  •  野的像风
    2020-11-22 03:54

    As Victor pointed out, the problem is with the alias. This can be avoided though, by putting the expression directly into the WHERE x IN y clause:

    SELECT `users`.`first_name`,`users`.`last_name`,`users`.`email`,SUBSTRING(`locations`.`raw`,-6,4) AS `guaranteed_postcode`
    FROM `users` LEFT OUTER JOIN `locations`
    ON `users`.`id` = `locations`.`user_id`
    WHERE SUBSTRING(`locations`.`raw`,-6,4) NOT IN #this is where the fake col is being used
    (
     SELECT `postcode` FROM `postcodes` WHERE `region` IN
     (
      'australia'
     )
    )
    

    However, I guess this is very inefficient, since the subquery has to be executed for every row of the outer query.

提交回复
热议问题