来自Mysql中HAVING子句的hasrsine距离计算的别名没有任何影响

时间:2016-06-29 19:41:17

标签: php mysql

这是我的代码(来源:在plumisland的Ollie):

$query = "SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
,111.045* DEGREES(ACOS(COS(RADIANS($latpoint))
             * COS(RADIANS(outcodepostcodes.lat))
             * COS(RADIANS($longpoint) - RADIANS(outcodepostcodes.lng))
            + SIN(RADIANS($latpoint))
             * SIN(RADIANS(outcodepostcodes.lat)))) AS distance_in_km
FROM $table     
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND 
(hide='0' OR hide IS NULL OR hide='')
HAVING distance_in_km <= 10
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";

每次旅行都有效,但是距离_in_km&lt; = 10&#39;没有任何影响。它不会将结果列表减少到10公里范围内。

我已经完成了PHP的距离计算,以显示每行结果的实际距离作为备份检查。

结果示例如下所示:

calc-dist is 57.8558548681, Entfernung ist 57.76155432
Lat=51.609922000000000, Lng=0.645703000000000   
1 Bedroom Flat For Sale £142
Hullbridge Hullbridge SS5 Essex England
Available with No Onward Chain is this purpose built Ground floor 
One Bedroom Apartment located in an attractive block within easy 
access of all village amenities and the River Crouch
More info/photos/map To discuss email agent (Prop: 3976)
Agent: Hullbridge Village and Park Homes

calc-dist is 557.289608887, Entfernung ist 556.381270123
Lat=56.094297000000000, Lng=-3.525096000000000  
2 Bedroom Flat For Sale £49,950
Sunnybraes Terrace Saline Steelend KY12 Fife Scotland
Spacious Upper Flat EPC - E
More info/photos/map To discuss email agent (Prop: 1995)
Agent: Able Agents

calc-dist is 566.191606711, Entfernung ist 565.268758382
Lat=56.115000000000000, Lng=-3.781000000000000  
1 Bedroom Flat For Sale £55,000
Mar Street Alloa Alloa FK10 Falkirk Scotland
Refurbished Flat EPC : D
More info/photos/map To discuss email agent (Prop: 4091)
Agent: Able Agents

calc-dist is 24.1715086823Entfernung ist 24.1321110012
Lat=51.479398000000000, Lng-0.179717000000000   
0 Bedroom Studio For Sale £59,995
Frobisher Road Erith DA8
ableestates.com/ LOW PRICE FOR QUICK SALE * IDEAL INVESTMENT OPPOTUNITY 
More info/photos/map To discuss email agent (Prop: 2133)
Agent: Able Estates"

这四个结果中没有一个应该存在,因为它们的距离大于距离_in_km&lt; = 10&#39;中规定的10km。子句。

calc-dist和Entfernung(距离德语)是使用两种不同版本的半胱氨酸公式在PHP中计算的距离。

还显示了Lat和Lng值

不可否认,这些PHP距离计算是在使用以下方法评估mysql结果后完成的:     $ NUM = mysql_numrows($结果);     $ I = 0;     while($ i&lt; $ num){

2 个答案:

答案 0 :(得分:0)

你没有来自your_table ..似乎你没有使用聚合功能所以你没有分组,为此你不应该使用但是在哪里(或者)

    "SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
    ,111.045* DEGREES(ACOS(COS(RADIANS($latpoint))
                 * COS(RADIANS(outcodepostcodes.lat))
                 * COS(RADIANS($longpoint) - RADIANS(outcodepostcodes.lng))
                + SIN(RADIANS($latpoint))
                 * SIN(RADIANS(outcodepostcodes.lat)))) AS distance_in_km
    LEFT JOIN outcodepostcodes
    FROM $table
    ON UPPER($table.postcode)=outcodepostcodes.outcode
    WHERE
    $where_no_and
    AND 
    (hide='0' OR hide IS NULL OR hide='')
    AND distance_in_km <= 10
    ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage"

你在查询中也有很多var并没有正确引用...如果你确定要对sql注入进行适当的清理,你可以使用串联查询

    "SELECT " . $table .".*, outcodepostcodes.lat, outcodepostcodes.lng
    ,111.045* DEGREES(ACOS(COS(RADIANS(". $latpoint ."))
                 * COS(RADIANS(outcodepostcodes.lat))
                 * COS(RADIANS(". $longpoint .") - RADIANS(outcodepostcodes.lng))
                + SIN(RADIANS(" . $latpoint " ))
                 * SIN(RADIANS(outcodepostcodes.lat)))) AS distance_in_km
    LEFT JOIN outcodepostcodes
    FROM " . $table . 
    "  ON UPPER(" . $table. " .postcode)=outcodepostcodes.outcode
    WHERE " .
    $where_no_and . 
    "  AND 
    (hide='0' OR hide IS NULL OR hide='')
    AND (outcodepostcodes.lat, outcodepostcodes.lng
    ,111.045* DEGREES(ACOS(COS(RADIANS(". $latpoint ."))
                 * COS(RADIANS(outcodepostcodes.lat))
                 * COS(RADIANS(". $longpoint .") - RADIANS(outcodepostcodes.lng))
                + SIN(RADIANS(" . $latpoint " ))
                 * SIN(RADIANS(outcodepostcodes.lat)))))  <= 10
    ORDER BY rent " . $reihenach . "  LIMIT  " .  $offset . ", " . $rowsPerPage 

答案 1 :(得分:0)

HAVING用于过滤GROUP BY结果。没有GROUP BY所以简单地忽略了HAVING。要按派生列值过滤结果,您必须在WHERE中重复表达式或在VIEW中封装查询并选择表单。