我有一个 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/