How to compare scientific notation and decimal numbers in mysql MIN() aggregate function?

这一生的挚爱 提交于 2019-12-13 07:02:13

问题


How can i apply mysql MIN() over mixed decimal value and scientific notation values. I need to get minimum distance using latitude longitude equation in a mysql query with MIN(). It is working if the distance are decimal. But it is not working properly, if some distance are too small like "5.89872212195226e-05"

The problem is MIN(4,5.89872212195226e-05) in a mysql query will always return '4'.

Here is the part of query

 MIN (
         (acos(sin((".$searchLat."*pi()/180)) * 
         sin((b_loc.locLatitude*pi()/180))+cos((".$searchLat."*pi()/180)) * 
         cos((b_loc.locLatitude*pi()/180))
         * cos(((".$searchlng."- b_loc.locLongitude)*pi()/180))))*180/pi())*60*1.1515
        ) as mindistance

Any solution? Thank You


回答1:


Cast to decimal should help :-

mysql> select cast( "5.89872212195226e-05"  as decimal(65,30));
+--------------------------------------------------+
| cast( "5.89872212195226e-05"  as decimal(65,30)) |
+--------------------------------------------------+
|                 0.000058987221219522600000000000 |
+--------------------------------------------------+

Example of comparison :-

mysql> select least( 4, cast("5.89872212195226e-05" as decimal(65,30)) );
+------------------------------------------------------------+
| least( 4, cast("5.89872212195226e-05" as decimal(65,30)) ) |
+------------------------------------------------------------+
|                           0.000058987221219522600000000000 |
+------------------------------------------------------------+

Example usage :-

MIN(cast( ...  as decimal(65,30)))


来源:https://stackoverflow.com/questions/8356104/how-to-compare-scientific-notation-and-decimal-numbers-in-mysql-min-aggregate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!