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’. 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. 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.
You can do something like this:
and rename every occurrence of
latinrequest.latrequestis now an alias from your table name: “virtually” the first you’re choosing from.