在 sqlite 中将 int 转换为 real

Sqlite 中的除法返回整数值

sqlite> select totalUsers/totalBids from
(select (select count(*) from Bids) as totalBids ,
(select count(*) from Users) as totalUsers) A;
1

我们可以对结果进行类型化以得到除法结果的真实值吗?

81290 次浏览

Just multiply one of the numbers by 1.0:

SELECT something*1.0/total FROM somewhere

That will give you floating point division instead of integer division.

select cast ( ( select 1 ) as real );

https://www.sqlite.org/lang_expr.html#castexpr

In Sqlite the division of an integer by another integer will always round down to the closest integer.

Therefore if you cast your enumerator to a float:

SELECT CAST(field1 AS FLOAT) / field2

or if you want to update column based on text column:

UPDATE table_with_fields SET real_field=cast(field_with_txt AS real)