douzhi9921 2012-10-20 02:21
浏览 23
已采纳

MySQL:帮助更改此查询中的列名称

I'm getting the wrong kind of column names from this query ... for example, I'm getting the column name meta_value for the "resource_email" and the "resource_phone" cells... they both have the column name of 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

Not sure how I need to change this query in order to get the column names I really need.

  • 写回答

1条回答 默认 最新

  • duanjia4220 2012-10-20 02:25
    关注

    Use 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 ...
    

    The "cells" you mention are actually tables (table aliases, to be exact)

    If the database does not accept column aliases equal to table aliases, you need to use different table aliases (or different column names).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件