且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLite3中使用除法的计算字段

更新时间:2023-02-01 10:01:07

这是因为你在做整数除法和 23/26=0.您需要强制它进行浮点除法.最简单的方法是将一个值与 1.0 相乘,如下所示:

It's because you are doing integer division and 23/26=0. You need to force it to do floating point division instead. The easiest way is to multiply one value with 1.0 like this:

SELECT id, passed, failed, ((passed * 1.0) / (passed + failed )) * 100 AS 'passedPC' 
FROM myData

或者,您可以使用 cast 运算符 强制 aa 列到 实数数据类型:

Alternatively you could use the cast operator to force a a column to the real data type:

SELECT id, passed, failed, cast(passed as real) / (passed + failed ) * 100 AS 'passedPC' 
FROM myData;

示例 SQL Fiddle 显示差异.