sql - postgresql 不返回特定数字标准的值

标签 sql postgresql real-datatype

我有一个 postgresql 数据库,如下所示:

+---------------+---------------- ------+------------ ---+
|  id (bigint)  |  name (varying(255))  |  price (real)  |
+---------------+---------------- ------+------------ ---+
|       1       |          name 1       |        0.33    |
+---------------+---------------- ------+------------ ---+
|       1       |          name 2       |        1.33    |
+---------------+---------------- ------+------------ ---+
|       1       |          name 3       |        1       |
+---------------+---------------- ------+------------ ---+

然后是我的查询结果:

SELECT * FROM my_table WHERE price = 1    -- OK (one row returned)
SELECT * FROM my_table WHERE price = 1.0  -- OK (one row returned)
SELECT * FROM my_table WHERE price = 1.33 -- FAIL (no row returned)
SELECT * FROM my_table WHERE price = 0.33 -- FAIL (no row returned)

当该值无法转换为非浮点值时,postgresql 不会返回任何行。

我不明白为什么。你有同样的问题吗?我该如何解决这个问题?

最佳答案

我看到的一个解决方案是使用显式转换为 real 数据类型:

SELECT * FROM my_table WHERE price = 0.33::real;
 id |  name  | price 
----+--------+-------
  1 | name 1 |  0.33

SELECT * FROM my_table WHERE price = 1.33::real;
 id |  name  | price 
----+--------+-------
  1 | name 2 |  1.33

根据documentation :

A numeric constant that contains neither a decimal point nor an exponent is initially presumed to be type integer if its value fits in type integer (32 bits); otherwise it is presumed to be type bigint if its value fits in type bigint (64 bits); otherwise it is taken to be type numeric. Constants that contain decimal points and/or exponents are always initially presumed to be type numeric.

请注意:

SELECT 1.33::numeric = 1.33::real;
 ?column? 
----------
 f
(1 row)

关于sql - postgresql 不返回特定数字标准的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095638/

相关文章:

mysql - 使用另一个 GROUP BY 时选择具有 MAX 值的行

mysql - Codeigniter 随机行 mysql 错误 - 类 CI_DB_mysqli_result 的对象无法转换为字符串

sql - 如何使用季度末日期进行数学计算?

format - gfortran - 实际输出是否允许未指定的十进制长度?

string - 从实数到字符串变量对话

MySQL - 链接多个 SQL 命令

PHP:一个查询中的两个表

postgresql - 如何在复合类型文字中指定 PostGIS 地理值?

java - 无法找到具有逻辑名称的列

type-conversion - 整数到实数转换函数