MySQL 在mysql中将varchar转换为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19702967/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:17:14  来源:igfitidea点击:

Casting varchar to int in mysql

mysql

提问by user1907859

I have a column airline_id which is varchar in the table route, now I want to copy that value into the column airline_id_int which has the type int. I can't get the syntax right though..

我有一个列 airline_id,它是表路由中的 varchar,现在我想将该值复制到类型为 int 的列 airport_id_int 中。虽然我无法获得正确的语法..

This is what I have:

这就是我所拥有的:

UPDATE route SET airline_id_int = CAST(airline_id, int);

回答by MusicLovingIndianGirl

You have to use the ASkeyword for CAST.

您必须将AS关键字用于CAST

update route set airline_id_int = cast(airline_id AS UNSIGNED)

You can use

您可以使用

update route set airline_id_int = cast(airline_id AS SIGNED)

as well.

以及。

回答by Obl Tobl

Try the following:

请尝试以下操作:

update route set airline_id_int = cast(airline_id AS UNSIGNED);

It's not possible to cast directly to int. If you need signed int, replace UNSIGNEDwith SIGNED.

无法直接投射到int. 如果您需要签名整数,请替换UNSIGNEDSIGNED.

回答by Frederic Close

update route set airline_id_int = cast(airline_id as signed);

or

或者

update route set airline_id_int = cast(airline_id as unsigned);

if it can be negative

如果它可以是负数

回答by naveen goyal

Try this

尝试这个

update route set airline_id_int = CONVERT(airline_id, UNSIGNED);
update route set airline_id_int = CONVERT(airline_id, SIGNED);