SQL 在sqlite中将int转换为real
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8305613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting int to real in sqlite
提问by vaichidrewar
Division in sqlite return integer value
在 sqlite 中除法返回整数值
sqlite> select totalUsers/totalBids from
(select (select count(*) from Bids) as totalBids ,
(select count(*) from Users) as totalUsers) A;
1
Can we typecast the result to get the real value of division result?
我们可以对结果进行类型转换以获得除法结果的真实值吗?
回答by NullUserException
Just multiply one of the numbers by 1.0
:
只需将其中一个数字乘以1.0
:
SELECT something*1.0/total FROM somewhere
That will give you floating point division instead of integer division.
这会给你浮点除法而不是整数除法。
回答by Adam Garner
In Sqlite the division of an integer by another integer will always round down to the closest integer.
在 Sqlite 中,一个整数除以另一个整数将始终向下舍入到最接近的整数。
Therefore if you cast your enumerator to a float:
因此,如果您将枚举数转换为浮点数:
SELECT CAST(field1 AS FLOAT) / field2
回答by mpb
select cast ( ( select 1 ) as real );
回答by Greg
or if you want to update column based on text column:
或者如果您想根据文本列更新列:
UPDATE table_with_fields SET real_field=cast(field_with_txt AS real)