如果行数为0,请更改参数,直到返回行-1个查询?

时间:2019-01-24 09:11:10

标签: mysql if-statement count

是否返回行的主要原因是由于$ maxDistance。默认是0.2。它经常返回结果。但是,在某些情况下,返回的结果不是0.3。

是否有可能在MySQL中使用IF和COUNT继续运行以下语句,以便如果不返回任何行,则$ maxDistance会尝试直到0.5才放弃?

SELECT property.paon, property.saon, property.street, property.propertyType, property.postcode, property.bedrooms, 
property.receptions, property.lastSalePrice, max(DATE(property.lastTransferDate)), epc.ADDRESS1, epc.TOTAL_FLOOR_AREA, epc.PROPERTY_TYPE 
, ACOS( SIN($lat*PI()/180)
* SIN(property.latitude * PI() / 180 ) 
+ COS($lat*PI()/180)
* COS(property.latitude  * PI() / 180 )
* COS(property.longitude * PI() / 180
- $lng * PI() / 180 )
) * 3957 as distance


FROM property property
INNER JOIN epc ON property.postcode = epc.POSTCODE AND CONCAT(property.paon, ', ', property.street) = epc.ADDRESS1 
WHERE property.lastTransferDate >= NOW() - INTERVAL $maxLastSaleDateYears YEAR
$typeSQL 
$buildSQL 
$minFloorSQL $maxFloorSQL


and property.longitude
BETWEEN $lng - ($maxDistance / abs(cos(radians($lat))*69))
AND $lng + ($maxDistance / abs(cos(radians($lat))*69))

AND property.latitude  
BETWEEN $lat - ($maxDistance / 69)
AND $lat + ($maxDistance / 69)

GROUP BY property.paon, property.postcode
HAVING distance < $maxDistance
ORDER BY distance ASC 
LIMIT 100

0 个答案:

没有答案