SQL语句中 根据FLOAT变量查询无结果

问题描述:

前几天用SQL语句根据FLOAT变量进行查询:

<span style="font-size:14px;">SELECT * 
FROM table_name
WHERE float_value = 0.0052;</span>

明明数据库中有对应的数据,

却查不出来。

原因探索:

经搜索,原来问题出在float类型的变量上:
float类型变量不能通过"="比较大小是否相等,

而应该通过不等号判断所在范围。

解决方案:

SQL语句应该这样写:

<span style="font-size:14px;">SELECT * 
FROM table_name
WHERE float_value <= 0.0052+0.0001
    AND float_value >= 0.0052-0.0001;</span>

参考:

http://stackoverflow.com/questions/8839460/why-doesnt-this-sql-query-return-any-results-comparing-floating-point-numbers

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/23559769

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();