尽管声明了MySQL列,但未找到该列?

尽管声明了MySQL列,但未找到该列?,mysql,select,Mysql,Select,此查询可用于: select d.*, (6371 * 3.1415926 * SQRT((:lat - dl.lat) * (:lat - dl.lat) + COS(:lat / 57.2957795) * COS(dl.lat / 57.2957795) * (:long - dl.long) * (:long - dl.long)) / 180) as xdistance from `dudes` as d left join `dude_locations` as dl on (dl.

此查询可用于:

select d.*,
(6371 * 3.1415926 * SQRT((:lat - dl.lat) * (:lat - dl.lat) + COS(:lat / 57.2957795) * COS(dl.lat / 57.2957795) * (:long - dl.long) * (:long - dl.long)) / 180) as xdistance
from `dudes` as d
left join `dude_locations` as dl on (dl.id_dude = d.id)
where 
(6371 * 3.1415926 * SQRT((:lat - dl.lat) * (:lat - dl.lat) + COS(:lat / 57.2957795) * COS(dl.lat / 57.2957795) * (:long - dl.long) * (:long - dl.long)) / 180) <= dl.distance
group by d.id
limit 20
我所要做的就是使相同的计算不会重复两次。这可能吗


任何帮助都将不胜感激。

您需要的查询可能是这样的:

where 
d.xdistance <= dl.distance
select d.*,
       MIN((6371 * 3.1415926 * SQRT((:lat - dl.lat) * (:lat - dl.lat) + COS(:lat / 57.2957795) * COS(dl.lat / 57.2957795) * (:long - dl.long) * (:long - dl.long)) / 180) ) as xdistance
from `dudes` as d left join
     `dude_locations` as dl
     on (dl.id_dude = d.id)
group by d.id
having xdistance <= min(dl.distance)
limit 20;

您的查询需要一些改进。您正在使用左联接,然后第二个表上的条件将其转换为内部联接。您在select中有距离计算,但group by不包含这些变量-因此您将得到一个不确定的结果。工作查询将提供我想要的确切结果。你会怎么写?您是否可以创建一个包含如何编写查询的答案?我不是想让你为我做我的工作,我只是好奇。它可能会给你现在想要的结果。但是,当您使用明确说明不起作用的功能时,您将面临代码在任何时候都无法起作用的风险。我认为您可以通过将距离计算放在像min.这样的运算符中来修复该查询,但它不起作用。我现在得到未知列d.distance。@inkd我想错误应该是未知列d.xdistance
select d.*,
       MIN((6371 * 3.1415926 * SQRT((:lat - dl.lat) * (:lat - dl.lat) + COS(:lat / 57.2957795) * COS(dl.lat / 57.2957795) * (:long - dl.long) * (:long - dl.long)) / 180) ) as xdistance
from `dudes` as d left join
     `dude_locations` as dl
     on (dl.id_dude = d.id)
group by d.id
having xdistance <= min(dl.distance)
limit 20;