将 bigint unsigned 列转换为 bigint signed 失败
Posted
技术标签:
【中文标题】将 bigint unsigned 列转换为 bigint signed 失败【英文标题】:Conversion of bigint unsigned column to bigint signed fails 【发布时间】:2021-06-23 12:27:12 【问题描述】: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
【问题讨论】:
标签有助于吸引可以帮助回答问题的人的注意力。从错误消息中可以清楚地看出,OP 使用的是 MariaDB,而不是 MySQL。这些是不同的软件产品。 MariaDB 于 2010 年开始作为 MySQL 的一个分支,但现在它有很多不同之处,以至于它应该被视为一个不同的、不兼容的产品。 【参考方案1】:首先,CAST
后面有一个空格,这可能会导致您的问题出现其他解析错误/问题。您必须使用CAST(...)
。其次,BIGINT SIGNED
类型是不允许的,请查看CAST(expr AS type)
的列表。当您需要签名号码时,请使用 SIGNED
或 SIGNED INTEGER
类型,如文档中所述:
类型可以是以下值之一:
[...]
签名 [整数]
有关如何使用CAST()
函数的示例请参阅以下查询(示例在 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)
【讨论】:
以上是关于将 bigint unsigned 列转换为 bigint signed 失败的主要内容,如果未能解决你的问题,请参考以下文章
特定列 -- 将数据类型 nvarchar 转换为 bigint 时出错
使用 mySQL 中的行聚合和操作将 BigInt 时间戳转换为真实日期
Mysql Query error: BIGINT UNSIGNED value is out of range in..解决方法