将 bigint unsigned 列转换为 bigint signed 失败

Conversion of bigint unsigned column to bigint signed fails

我在 MySql 数据库版本 7.0

中遇到语法错误
SELECT
    r.id,
    r.number,
    r.numbertype,
    r.forhandler,
    LAG(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number) AS last_row_number,
    LEAD(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number) AS next_row_number,
    r.number -(LAG(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number)) AS gap_last_rk,
    CAST (r.number-(LEAD(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.`number`)) AS BIGINT SIGNED)  AS gap_next_rk

FROM admin.numberranges r
WHERE r.status=2
ORDER BY r.number;

语法错误在我的 CAST 部分。我的列 NUMBER 是一个 BIG INT UNSIGNED。

我也试过转换-:(

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BIGINT SIGNED) AS neg_number

首先,您在 CAST 之后有一个 space,这可能会导致其他 errors/issues 解析您的问题。你必须使用 CAST(...)。其次,类型 BIGINT SIGNED 不被允许,检查列表中的 CAST(expr AS type)。当你想要一个带符号的数字时,你可以使用 SIGNEDSIGNED INTEGER 类型,如文档中所述:

The type can be one of the following values:

[...]

  • SIGNED [INTEGER]

请参阅以下有关如何使用 CAST() 函数的查询(示例 运行 on MySQL 8.0.23,MariaDB 的结果可能不同,但类型限制都差不多,见MySQL documentation of CONVERT(expr, type)):

mysql> EXPLAIN Dummy;
+-------+-----------------+------+-----+---------+-------+
| Field | Type            | Null | Key | Default | Extra |
+-------+-----------------+------+-----+---------+-------+
| Test  | bigint unsigned | YES  |     | NULL    |       |
+-------+-----------------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> SELECT Test, CAST(Test AS BIGINT SIGNED) FROM Dummy;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
                    corresponds to your MySQL server version for the right syntax
                    to use near 'BIGINT SIGNED) FROM Dummy' at line 1
mysql> SELECT Test, CAST(Test AS SIGNED) FROM Dummy;
+------+----------------------+
| Test | CAST(Test AS SIGNED) |
+------+----------------------+
| 1234 |                 1234 |
+------+----------------------+
1 row in set (0.00 sec)