doubi8383 2018-05-23 06:21
浏览 101
已采纳

在sql查询中使用带有AND的'<'符号时遇到的问题

I'm getting issue when I try to use < symbol with AND condition in sql query.

   WHERE (latitude - 0.009 < AND latitude + 0.00 >)
           AND (longitude - 0.009 < 
                AND longitude + 0.009 > )

Here is my code in CorePHP:

<?php
ini_set("display_errors", 0);

$lat = $_REQUEST['latit'];
$long = $_REQUEST['longit'];

include ("commanvar.php");
include ("class.db.php");

$coordinates_ = '';

// function to calculate distance between two latitudes and longitudes
function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
    $pi80 = M_PI / 180;
    $lat1 *= $pi80;
    $lng1 *= $pi80;
    $lat2 *= $pi80;
    $lng2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;
    $dlng = $lng2 - $lng1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    return ($miles ? $km * 0.621371192 : $km);
}

$obj = new db_connect();

// added by pramod

$sql = "SELECT name,
       latitude,
       longitude,
       TYPE
  FROM (SELECT name,
                TO_BINARY_FLOAT(latitude) latitude,
                TO_BINARY_FLOAT(longitude) longitude,
               TYPE
          FROM (SELECT name,
                       latitude,
                       longitude,
                       TYPE,
                       is_number (latitude) latisnum,
                       is_number (longitude) longisnum
                  FROM (SELECT name,
                               REGEXP_SUBSTR (latlon,
                                              '[^,]+',
                                              1,
                                              1)
                                  latitude,
                               REGEXP_SUBSTR (latlon,
                                              '[^,]+',
                                              1,
                                              2)
                                  longitude,
                               TYPE
                          FROM (SELECT olt.name,
                                       olt_details.latlon,
                                       'olt' AS TYPE
                                  FROM ftth.olt, ftth.olt_details
                                 WHERE olt_id = id
                                UNION
                                SELECT name, latlon, TYPE FROM ftth.splitters
                                ))
                 WHERE latitude IS NOT NULL AND longitude IS NOT NULL)
         WHERE latisnum = 1 AND longisnum = 1)
 WHERE (latitude - 0.009 < $lat
        AND latitude + 0.00 > $lat)
       AND (longitude - 0.009 < $long
            AND longitude + 0.009 > $long)";
//die();

$obj->db_query($sql);

// echo $sql;

// echo $lat . ',' . $long;
// define json array coordinates and prepare it's elements for returning via AJAX

$coordinates = '{
"coordinates": [';

while ($result = $obj->db_fetch_array(1)) {

    $latitude = $result['LATITUDE'];
    $longitude = $result['LONGITUDE'];
    $name = $result['NAME'];
    $type = $result['TYPE'];

    $latlon_fiber = $result['LATITUDE'] . ", " . $result['LONGITUDE'];

    $distance_fromswitch = distance($lat, $long, $latitude, $longitude, FALSE);
    $distance_fromswitch = floor($distance_fromswitch * 1000);

    $coordinates_ .= '{ "distance":"' . $distance_fromswitch . '" ,"site_name":"' . $name . '" , "latitude":"' . $latitude . '" , "longitude":"' . $longitude . '" , "device_type":"' . $type . '" },';
}

$coordinates .= rtrim($coordinates_, ',');

$coordinates .= "]}";

echo $coordinates;
$obj->free();
?>

I'm getting the following error due to this part < AND on line WHERE (latitude - 0.009 < AND latitude + 0.00 >)

ORA-00936: missing expression
00936. 00000 -  "missing expression"

I think I've used some improper syntax while using < with AND on line WHERE (latitude - 0.009 < AND latitude + 0.00 >)

What can be the correction that need to be done here?

  • 写回答

1条回答 默认 最新

  • duanqiao1947 2018-05-23 06:28
    关注

    Think of it! The error message is self-explanatory.

    You're trying to compare the result of subtraction to nothing. To correct this, you have to change it to:

    WHERE (latitude - 0.009 < SomeValueHere AND latitude + 0.00 > SomeValueHere)
    

    For further details, please see: ORA-00936 missing expression

    [EDIT]

    As to changes made in the question...

    You're asking for help in debugging (inspect) your code. Let me quote the comment to your comment:

    probably the variables do not have any values and passing empty strings into your query.. – skybunk

    Thank you, @skybunk!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM