简体   繁体   English

2个相同的查询数据库返回不同的结果

[英]2 same queries database return different results

I'm using Yii2 ActiveRecord & ActiveQuery to retrieve data from database, but I'm facing different results: 我正在使用Yii2 ActiveRecord和ActiveQuery从数据库检索数据,但是我面临着不同的结果:

    $ne_lat = 53.42712825923;
    $sw_lat = 53.39008549731;
    $ne_lon = -2.1651649475098;
    $sw_lon = -2.288761138916;

    $poi = Point::find()

        ->andWhere('latitude <= :ne_lat', [':ne_lat' => $ne_lat])
        ->andWhere('latitude >= :sw_lat', [':sw_lat' => $sw_lat])
        ->andWhere('longitude <= :ne_lon', [':ne_lon' => $ne_lon])
        ->andWhere('longitude >= :sw_lon', [':sw_lon' => $sw_lon])
        ->andWhere(['status' => 1])
        ->asArray()
        ->all();

returns 0 an empty array. 返回0的空数组。 But same query get just by a little bit different way: 但是,相同的查询只是有点不同:

   $poi = Point::findBySql('SELECT * 
      FROM `point` 
      WHERE (((((latitude <= 53.42712825923) 
      AND (latitude >= 53.39008549731)) 
      AND (longitude <= -2.1651649475098)) 
      AND (longitude >= -2.288761138916)) 
      AND (`status`=1))
   ')->asArray()->all();

returns 30+ results. 返回30多个结果。 Results look like this: 结果如下:

id,          name,         latitude,         longitude
1 ,        point1,       53.3917409,        -2.1684337    
2 ,        point2,       53.4135577,        -2.1673014    
3 ,        point3,       53.3991094,        -2.2513453    

It looks like there is something wrong with binding floats because 绑定浮动似乎有问题,因为

  $sql = "
        SELECT *
        FROM point
        WHERE latitude <= :ne_lat
          AND latitude >= :sw_lat
          AND longitude <= :ne_lon
          AND longitude >= :sw_lon
          AND status=1
    ";

    $poi = Fitter::findBySql($sql, [':ne_lat' => $ne_lat, ':sw_lat' => $sw_lat, ':ne_lon' => $ne_lon, ':sw_lon' => $sw_lon])->asArray()->all();

Also doesn't return any record 也不会返回任何记录

Does anybody know why? 有人知道为什么吗?

$sw_lon = 2.288761138916; $ sw_lon = 2.288761138916; <-- this is positive in the first query while in the second it is negative (-2.288761138916). <--在第一个查询中为正,而在第二个查询中为负(-2.288761138916)。

if I'm not wrong, using the find() method will return you this query statement. 如果我没看错,则使用find()方法将返回此查询语句。

SELECT * FROM point WHERE (atitude <= 53.42712825923) AND (latitude >= 53.39008549731) AND (longitude <= -2.1651649475098) AND (longitude >= -2.288761138916) AND ( status =1) point (海拔<= 53.42712825923)和(纬度> = 53.39008549731)和(经度<= -2.1651649475098)和(经度> = -2.288761138916)和( status = 1)

And not 并不是

SELECT * FROM point WHERE (((((latitude <= 53.42712825923) AND (latitude >= 53.39008549731)) AND (longitude <= -2.1651649475098)) AND (longitude >= -2.288761138916)) AND ( status =1)) point选择*((((((latitude <= 53.42712825923)AND(latitude> = 53.39008549731))AND(经度<= -2.1651649475098))AND(经度> = -2.288761138916)AND( status = 1))

The difference in sql statement might be the cause. sql语句中的差异可能是原因。

您可以在第一个查询构建器中尝试用“状态= 1”代替“状态” => 1吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一查询,同一数据库,不同结果 - Same Query, Same Database, Different Results 2个相同的apache / php安装从同一个SQL Server数据库返回不同的结果 - 2 identical installations of apache/php return different results from the same SQL Server database PHP-搜索数据库并在同一页面上返回结果 - PHP - Search database and return results on the same page 使用GoogleAPI返回2个不同查询的搜索结果数。 (PHP) - Using the GoogleAPI to return the number of search results for 2 different queries. (PHP) Laravel:如何将 2 个不同的数据库查询合并为一个并访问结果? - Laravel: How to merge 2 different database queries into one and access the results? 连接查询以得到不同的结果 - Joining the queries for different results PHP MySQL查询未返回任何结果 - PHP MySQL queries return no results PHP和Python解压缩从同一来源返回不同的结果 - PHP and Python unpack return different results from same source 相同的代码在浏览器和localhost / phpmyadmin(database)中产生不同的结果 - The same code produces different results in browser and localhost/phpmyadmin(database) 几次成功调用后,php PDO数据库hande拒绝将结果返回查询 - php PDO Database hande refuses to return results to queries after, several, succesful calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM