$distance = DB::select(
                DB::raw("
                SELECT a.distance
                FROM ( SELECT user_id, street, store_name,
                    (
                        '".$mean_radius_of_Earth."' *
                        acos(cos(radians('".$latitude."')) *
                        cos(radians(latitude)) *
                        cos(radians(longitude) -
                        radians('".$longitude."')) +
                        sin(radians('".$latitude."')) *
                        sin(radians(latitude)))
                    ) AS distance
                    FROM addresses ) AS a
                    WHERE id= '". $address_id."'

                ")
            );


我必须使用此address_id作为主键来获取特定行的所有列,但出现错误:-未找到列:1054“ where子句”中的未知列“ id”,但是当我尝试实现此目的时与user_id然后我得到所有字段数据,user_id(不是primary_key),您的帮助将不胜感激,
在此先感谢,对不起英语不对。

最佳答案

您没有在子请求中选择ID,因此,或者在子查询中进行选择,或者在子选择中选择字段ID:

$distance = DB::select(
            DB::raw("
            SELECT a.distance
            FROM ( SELECT user_id, street, store_name,
                (
                    '".$mean_radius_of_Earth."' *
                    acos(cos(radians('".$latitude."')) *
                    cos(radians(latitude)) *
                    cos(radians(longitude) -
                    radians('".$longitude."')) +
                    sin(radians('".$latitude."')) *
                    sin(radians(latitude)))
                ) AS distance
                FROM addresses
                WHERE id= '". $address_id."') AS a

            ")
        );

关于php - 如何从具有经度和纬度的mysql地址表中获取经度和纬度的距​​离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50878047/

10-09 04:39