相同的请求MySQL/PHP不同的结果

时间:2020-03-20 作者:ilanb

我有一个PHP查询:

$querystrShop = $wpdb->get_results("
                        SELECT productid 
                        AS post_id,( 3959 * acos(cos(radians($latitude)) * cos( radians(latitude))* cos( radians( longitude )- radians($longitude))+ sin(radians($latitude))* sin( radians(latitude)))) 
                        AS distance, phiz_postmeta.meta_value
                        FROM phiz_geo_location
                        JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
                        LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
                        LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
                        WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
                        AND p.post_title =\'".$html."\'
                        AND p.post_status = \'publish\'
                        HAVING distance < \'100\'
                        ");
这个Mysql查询:

SELECT productid AS post_id,( 3959 * acos(cos(radians(43.5586)) * cos( radians(latitude))* cos( radians( longitude )- radians(5.25182))+ sin(radians(43.5586))* sin( radians(latitude)))) AS distance, phiz_postmeta.meta_value FROM phiz_geo_location JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id WHERE phiz_term_relationships.term_taxonomy_id IN (207) AND p.post_title = \'ACTION PARIS MASQ NOIR PEEL OFF X3\' AND p.post_status = \'publish\'HAVING distance < \'100\'
第一个要求有3个产品,第二个只有2个,为什么会有这种差异?

我尝试获取范围距离(100)内的所有产品,实际上PHP查询返回

距离:191公里

距离:18.3公里

距离:11.3公里

和Mysql返回

距离:18.3公里

距离:11.3公里

谢谢

编辑:

添加了一些更正,但存在相同的问题:

$querystrShop = $wpdb->prepare("
                        SELECT productid 
                        AS post_id,( 3959 * ACOS(COS(RADIANS( $latitude )) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS( $longitude )) + SIN(RADIANS( $latitude )) * SIN(RADIANS(phiz_geo_location.latitude))))
                        AS distance, phiz_postmeta.meta_value
                        FROM phiz_geo_location
                        JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
                        LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
                        LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
                        WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
                        AND p.post_title =\'".$html."\'
                        AND p.post_status = \'publish\'
                        HAVING distance < \'100\'
                        ");

                        $querystrShop = $wpdb->get_results( $querystrShop );
编辑2:也已尝试

$querystrShop = $wpdb->prepare("
                        SELECT productid 
                        AS post_id,( %d * ACOS(COS(RADIANS( %s )) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS( %s )) + SIN(RADIANS( %s )) * SIN(RADIANS(phiz_geo_location.latitude))))
                        AS distance, phiz_postmeta.meta_value
                        FROM phiz_geo_location
                        JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
                        LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
                        LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
                        WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
                        AND p.post_title =\'".$html."\'
                        AND p.post_status = \'publish\'
                        HAVING distance < %f",
                        $earth_radius,
                        $latitude,
                        $longitude,
                        $latitude,
                        $radious
                        );

                        $sql = $wpdb->get_results( $querystrShop );
编辑3准备的结果:

SELECT productid AS post_id,( 3959 * ACOS(COS(RADIANS(\'43.2832512\')) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS(\'5.5115776\')) + SIN(RADIANS(\'43.2832512\')) * SIN(RADIANS(phiz_geo_location.latitude)))) AS distance, phiz_postmeta.meta_value 
FROM phiz_geo_location 
JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id 
LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id 
LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id 
WHERE phiz_term_relationships.term_taxonomy_id IN (207) 
AND p.post_title =\'ACTION PARIS MASQ NOIR PEEL OFF X3\' 
AND p.post_status = \'publish\' 
HAVING distance < 100

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

它们给出不同的结果,因为这些查询不同。它们有不同的纬度和经度。

例如,其中一个纬度为“43.2832512”,而另一个纬度为43.5586。它们甚至不是相同的数据类型。

prepare语句中的long/lat用于马赛以东的一个公园,而SQL查询中的那一个位于西北约36公里处的库杜镇:

enter image description here