如何在 MySQL 中将类型转换为 bigint?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4660383/
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
How do I cast a type to a bigint in MySQL?
提问by Anonym
CAST()
seems to only work for BINARY,CHAR,DATE;DATETIME,DECIMAL,TIME,SIGNED,UNSIGNED.
CAST()
似乎只适用于 BINARY,CHAR,DATE;DATETIME,DECIMAL,TIME,SIGNED,UNSIGNED。
I need to convert a hex string to a bigint, that is, I'd want:
我需要将十六进制字符串转换为 bigint,也就是说,我想要:
SELECT CAST(CONV("55244A5562C5566354',16,10) AS BIGINT)
CONV() returns a string, so that's why I'm trying the convert it. I have 2 uses for this
CONV() 返回一个字符串,所以这就是我尝试转换它的原因。我有两个用途
Inserting data, e.g.
INSERT INTO a(foo) SELECT CONV(bar,16,10) FROM ...
Here foo is a bigint column, bar a varchar. Perhaps I could get away with the select statement being a string and let MySQL take care of it (?)Returning data where the client will dynamically learn the data type of the column,
SELECT CONV(bar,16,10)
is no good as the client will handle it as a string.
插入数据,例如
INSERT INTO a(foo) SELECT CONV(bar,16,10) FROM ...
这里 foo 是一个 bigint 列,bar 是一个 varchar。也许我可以将 select 语句作为一个字符串,让 MySQL 处理它(?)返回客户端将动态学习列的数据类型的数据
SELECT CONV(bar,16,10)
是不好的,因为客户端会将其作为字符串处理。
回答by Quassnoi
SELECT CAST(CONV('55244A5562C5566354',16,10) AS UNSIGNED INTEGER);
回答by vartec
What seems to be the problem? I've tested this conversion both on 64-bit and 32-bit system. Works fine. Note, that instead of doing hex to bin conversion, you can just treat the number as hexadecimal.
似乎是什么问题?我已经在 64 位和 32 位系统上测试了这种转换。工作正常。请注意,您可以将数字视为十六进制,而不是进行十六进制到 bin 的转换。
mysql> SELECT CAST(X'55244A5562C5566354' AS UNSIGNED);
+-----------------------------------------+
| CAST(X'55244A5562C5566354' AS UNSIGNED) |
+-----------------------------------------+
| 2614996416347923284 |
+-----------------------------------------+
1 row in set (0.00 sec)