我有一个问题,我需要在 MySQL 查询中作为列并在该列上执行 BETWEEN。我能想出的最简单的版本说明了这个问题:
SELECT ID AS post_id FROM wp_posts WHERE (post_id BETWEEN 10 AND 20)
这会引发以下错误:
#1054 - Unknown column 'post_id' in 'where clause'
有没有办法让新创建的post_id
列对 BETWEEN 运算符可见?
更新:
这是我的实际查询。如您所见,它稍微复杂一些:
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_status='publish'
AND
wp_posts.post_type='store'
GROUP BY wp_postmeta.post_id