[英]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.