简体   繁体   English

#1052-在英里半径查询中,字段列表中的“纬度”列含糊不清,无法弄清原因

[英]#1052 - Column 'lat' in field list is ambiguous in a mile radius query, can't figure out why

I've got a rather lengthy query i have been working with that is throwing the error '#1052 - Column 'lat' in field list is ambiguous'. 我一直在处理一个相当长的查询,该查询抛出错误“#1052-字段列表中的列”纬度”不明确”。 I have broken it into parts and each part seems to work fine but when I run it at once I get this error. 我把它分成几部分,每个部分似乎都可以正常工作,但是当我立即运行它时,我得到了这个错误。 Here is the query: 这是查询:

    SELECT lesson_requests_global_2.student_name,
           (3959 * ACOS(COS(RADIANS(30.096595)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(- 81.718983)) + SIN(RADIANS(30.096595)) * SIN(RADIANS(lat)))) AS distance,
           lesson_requests_vendor.user_purchased
    FROM lesson_requests_global_2
    INNER JOIN
      ( SELECT student_name,
               MAX(request_date) AS max_request_date
       FROM lesson_requests_global_2
       WHERE ( 3959 * ACOS(COS(RADIANS(30.096595)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(- 81.718983)) + SIN(RADIANS(30.096595)) * SIN(RADIANS(lat))) ) < 30
       GROUP BY student_name ) AS recent_student_lesson_request ON lesson_requests_global_2.student_name = recent_student_lesson_request.student_name
    AND lesson_requests_global_2.request_date = recent_student_lesson_request.max_request_date
    LEFT JOIN lesson_requests_vendor ON v.user_purchased = lesson_requests_global_2.student_name
    WHERE lesson_requests_vendor.user_purchased <> 'bob jones'
      AND distance < 30
    ORDER BY distance LIMIT 0 , 20

Please note that the long COS/RADIANS stuff looks complicated but it is to find a mile radius distance. 请注意,很长的COS / RADIANS东西看起来很复杂,但是要找到半径一英里的距离。 I think that somehow it is thinking 'lat' within those formulas is somehow in the column list? 我认为以某种方式认为这些公式中的“纬度”是否在列列表中?

Thanks in advance for your help! 在此先感谢您的帮助!

Is very simple. 很简单。

You join on the same table from where you select, so you'll have two column with the same name. 您从选择的位置连接到同一张表,因此将有两列具有相同的名称。 If you don't put the "table name" before your field name, this will produce a sql error. 如果您未在字段名称之前放置“表名称”,则会产生sql错误。

You can do something like this: 您可以执行以下操作:

SELECT .... FROM lesson_requests_global_2 request
INNER JOIN
( SELECT ..... FROM lesson_request_globals_2 .....)
....
WHERE ....

and rename every occurrence of lat in request.lat 并重命名request.lat每次出现的lat

request is now an alias from your table name: "virtually" the first you're choosing from. 现在, request是表名的别名:“实际上”是您从中选择的第一个。

Sounds like both lesson_requests_global_2 and lesson_requests_vendor have a column called 'lat'. 听起来像lesson_requests_global_2和lesson_requests_vendor都有一个名为“ lat”的列。 You need to specify which table you want to query it from: 您需要指定要查询的表:

SELECT lesson_requests_global_2.student_name,
       (3959 * ACOS(COS(RADIANS(30.096595)) * COS(RADIANS(lesson_requests_global_2.lat)) * COS(RADIANS(lng) - RADIANS(- 81.718983)) + SIN(RADIANS(30.096595)) * SIN(RADIANS(lat)))) AS distance,
       lesson_requests_vendor.user_purchased
FROM lesson_requests_global_2
INNER JOIN
  ( SELECT student_name,
           MAX(request_date) AS max_request_date
   FROM lesson_requests_global_2
   WHERE ( 3959 * ACOS(COS(RADIANS(30.096595)) * COS(RADIANS(lesson_requests_global_2.lat)) * COS(RADIANS(lng) - RADIANS(- 81.718983)) + SIN(RADIANS(30.096595)) * SIN(RADIANS(lesson_requests_global_2.lat))) ) < 30
   GROUP BY student_name ) AS recent_student_lesson_request ON lesson_requests_global_2.student_name = recent_student_lesson_request.student_name
AND lesson_requests_global_2.request_date = recent_student_lesson_request.max_request_date
LEFT JOIN lesson_requests_vendor ON v.user_purchased = lesson_requests_global_2.student_name
WHERE lesson_requests_vendor.user_purchased <> 'bob jones'
  AND distance < 30
ORDER BY distance LIMIT 0 , 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM