sqlite – 类型转换问题

前端之家收集整理的这篇文章主要介绍了sqlite – 类型转换问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在以下查询中,1/2的答案始终为零,而应为0.5.你能告诉我如何将两个int列转换为float / double吗?
SELECT 1/2,total,sb_ondate,likes
  FROM (
      SELECT sb_ondate,COUNT(*) AS total,STRFTIME("%w",sb_ondate) AS weekDay,COUNT(CASE WHEN sb_reaction = 'like' THEN sb_id END) AS likes
        FROM diet
    GROUP BY weekDay) AS f;

谢谢.

将两个除数中的一个作为一个实数:
SELECT 1 / CAST(2 AS REAL),...

示例交互式Python会话:

>>> import sqlite3
>>> c = sqlite3.connect(':memory:')
>>> c.execute("create table t (a int,b int);")
<sqlite3.Cursor object at 0x7f9b0e9539d0>
>>> c.execute("insert into t values (1,2)")
<sqlite3.Cursor object at 0x7f9b0e953ad8>
>>> conn.execute("select a / cast(b as real) from t").fetchone()
(0.5,)

猜你在找的Sqlite相关文章