簡體   English   中英

MySQL:幫助更改此查詢中的列名

[英]MySQL: Help changing the column names in this query

我發現了一種錯誤的列名從該查詢...例如,我發現了列名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

不知道我該如何更改此查詢才能獲取我真正需要的列名。

使用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 ...

您提到的“單元格”實際上是表(確切地說是表別名)

如果數據庫不接受等於表別名的列別名,則需要使用不同的表別名(或不同的列名)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM