0

我从这个查询中得到了错误的列名......例如,我得到了meta_value“resource_email”和“resource_phone”单元格的列名......它们的列名都是meta_value

SET @centerLat = '48.428289';
SET @centerLng = '-123.380585';

SELECT wp_posts.*, resource_email.meta_value, resource_phone.meta_value,
( 3959 * acos( cos( radians( @centerLat ) ) * cos( radians( lat.meta_value ) ) * cos( radians( lng.meta_value ) - radians(@centerLng) ) + sin( radians( @centerLat ) ) * sin( radians( lat.meta_value ) ) ) ) AS distance

FROM wp_posts

LEFT JOIN wp_postmeta AS lat 
    ON lat.post_id = wp_posts.id 
        AND lat.meta_key = 'bid_resource_lat'

LEFT JOIN wp_postmeta AS lng 
    ON lng.post_id = wp_posts.id 
        AND lng.meta_key = 'bid_resource_lng'

LEFT JOIN wp_postmeta AS resource_email 
    ON resource_email.post_id = wp_posts.id 
        AND resource_email.meta_key = 'bid_resource_primary_email'

LEFT JOIN wp_postmeta AS resource_phone 
    ON resource_phone.post_id = wp_posts.id 
        AND resource_phone.meta_key = 'bid_resource_primary_phone'

HAVING distance < 5
LIMIT 0 , 20

不确定我需要如何更改此查询才能获得我真正需要的列名。

4

1 回答 1

0

使用AS

SELECT wp_posts.*,
  resource_email.meta_value AS resource_email,
  resource_phone.meta_value AS resource_phone,
  ...
FROM ...
LEFT JOIN wp_postmeta AS resource_email ...
LEFT JOIN wp_postmeta AS resource_phone ...

您提到的“单元格”实际上是表格(准确地说是表格别名)

如果数据库不接受等于表别名的列别名,则需要使用不同的表别名(或不同的列名)。

于 2012-10-20T02:25:53.420 回答