douwu7168 2014-01-25 20:56
浏览 367

MySQL查询以确定数字是否大于包含负值的VARCHAR

For a project, I'm storing location points in my MySQL database. The latitude and longitude data for each point is stored as a varchar. It must remain a varchar for reasons I won't get in to. For each new point, I'm trying to perform a search to see if there's already any point in my database within a .000300 latitude/longitude radius of the new point.

My PHP code is as follows: ($lat and $long are the new point's latitude and longitude)

$lat1 = $lat - 0.000300;
$lat2 = $lat + 0.000300;
$long1 = $long - 0.000300;
$long2 = $long + 0.000300;

$sql = "SELECT * FROM `DataBase`.`DataBase_data` WHERE `DataBase_data`.`Type`='Point' AND `DataBase_data`.`Lat`>'$lat1' AND `DataBase_data`.`Lat`<'$lat2' AND `DataBase_data`.`Long`>'$long1' AND `DataBase_data`.`Long`<'$long2'";
$query = mysql_query($sql);
echo mysql_error();

The code works correctly when both latitude and longitude are positive numbers. However, when one (or both) of them are negative numbers, the search doesn't work. I think it has something to do with doing a "greater than" and "less than" comparison with a varchar containing a - sign, but I'm not positive. Any ideas about how to fix the problem would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doumo1807831 2014-01-25 21:08
    关注

    This is your query

    SELECT d.*
    FROM `DataBase`.`DataBase_data` d
    WHERE d.`Type`='Point' AND d.`Lat`> '$lat1' AND d.`Lat`<'$lat2' AND
          d.`Long`>'$long1' AND d.`Long`<'$long2';
    

    My first suggestion is to do the comparison as numbers. You can convert to a number easily in MySQL by adding 0:

    SELECT d.*
    FROM `DataBase`.`DataBase_data` d
    WHERE d.`Type` = 'Point' AND (d.`Lat` + 0) > $lat1 AND (d.`Lat` + 0) < $lat2 AND
          (d.`Long` + 0) > $long1 AND (d.`Long` + 0) < $long2;
    

    Does this fix your problem?

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用