开发者

MySQL column AS with BETWEEN causes error

I have an issue where I need to AS a column in a MySQL query and execute a BETWEEN on that column. The simplest version I can come up with that illustrates the problem is this:

SELECT ID AS post_id FROM wp_posts WHERE (post_id BETWEEN 10 AND 20)

This throws the following error:

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

Is there a way to make the newly created post_id column visible to the BETWEEN operator?

UPDATE:

This is my actual query. As you can see, it is slightly more complicated:

SELECT wp_postmeta.post_id,
MAX(CASE WHEN wp_postmeta.meta_key='store_lng' THEN wp_postmeta.meta_value END ) AS lng,
MAX(CASE WHEN wp_postmeta.meta_key='store_lat' THEN wp_postmeta.meta_value END ) AS lat
FROM wp_postmeta
LEFT JOIN wp_posts ON (wp_posts.ID=wp_postmeta.post_id) 
WHERE
(lng BETWEEN 150.29793837662 AND 152.1161201948)
AND 
(lat BETWEEN -34.775666623377 AND -32.957484805195) 
AND
wp_posts.post_st开发者_开发技巧atus='publish'
AND
wp_posts.post_type='store'
GROUP BY wp_postmeta.post_id


From the MySQL documentation:

It is not permissible to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed.

You'll either have to use ID instead of post_id in the WHERE clause or use a HAVING clause.

For example:

SELECT ID AS post_id FROM wp_posts WHERE ID BETWEEN 10 AND 20

Update: Based on your most recent update, you could use a subquery as already suggested or as I suggested earlier, you could use a HAVING clause.

For example:

SELECT    wp_postmeta.post_id,
          MAX(CASE WHEN wp_postmeta.meta_key = 'store_lng' THEN wp_postmeta.meta_value END) AS lng,
          MAX(CASE WHEN wp_postmeta.meta_key = 'store_lat' THEN wp_postmeta.meta_value END) AS lat
FROM      wp_postmeta
LEFT JOIN wp_posts
ON        wp_posts.ID = wp_postmeta.post_id
WHERE     wp_posts.post_status = 'publish' AND
          wp_posts.post_type = 'store'
GROUP BY  wp_postmeta.post_id
HAVING    lng BETWEEN 150.29793837662 AND 152.1161201948 AND 
          lat BETWEEN -34.775666623377 AND -32.957484805195


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. (http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html)


Yes, you can!

SELECT post_id
from (select id AS post_id FROM wp_posts) x
WHERE post_id BETWEEN 10 AND 20

Edited to include changes to question. Try this (haven't run in mysql, but should be close if not correct):

SELECT *
FROM (SELECT
  wp_postmeta.post_id,
  MAX(CASE WHEN wp_postmeta.meta_key='store_lng' THEN wp_postmeta.meta_value END ) AS lng,
  MAX(CASE WHEN wp_postmeta.meta_key='store_lat' THEN wp_postmeta.meta_value END ) AS lat 
FROM wp_postmeta
GROUP BY wp_postmeta.post_id
) x
LEFT JOIN wp_posts ON wp_posts.ID = x.post_id
WHERE lng BETWEEN 150.29793837662 AND 152.1161201948
AND lat BETWEEN -34.775666623377 AND -32.957484805195
AND wp_posts.post_status='publish'
AND wp_posts.post_type='store'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜