0

我有一个相当长的查询,我一直在处理它抛出错误'#1052 - 字段列表中的列'lat'不明确'。我把它分成几部分,每个部分似乎都工作正常,但是当我一次运行它时,我得到了这个错误。这是查询:

    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

请注意,长 COS/RADIANS 的东西看起来很复杂,但它是找到一英里半径的距离。我认为它以某种方式认为这些公式中的“纬度”以某种方式在列列表中?

在此先感谢您的帮助!

4

2 回答 2

0

很简单。

您从您选择的地方加入同一张表,因此您将拥有两个具有相同名称的列。如果你没有把“表名”放在你的字段名之前,这会产生一个sql错误。

你可以这样做:

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

并重命名每次出现的latinrequest.lat

request现在是您的表名的别名:“实际上”是您选择的第一个。

于 2012-06-15T15:00:08.250 回答
0

听起来 course_requests_global_2 和 course_requests_vendor 都有一个名为“lat”的列。您需要指定要从哪个表中查询它:

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
于 2012-06-15T15:00:11.387 回答