在 MySQL 中 int 到字符串

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

int to string in MySQL

mysql

提问by Andreas L.

Is it possible to do something like this? Essentially I want to cast a int into a string and used the string on a join. Pay attention to the %t1.id%

有可能做这样的事情吗?本质上,我想将 int 转换为字符串并在连接上使用该字符串。注意事项%t1.id%

select t2.* 
from t1 
join t2 on t2.url='site.com/path/%t1.id%/more' 
where t1.id > 9000

回答by Andreas L.

If you have a column called "col1" which is int, you cast it to String like this:

如果您有一列名为“col1”的 int 列,则可以像这样将其转换为 String:

CONVERT(col1,char)

e.g. this allows you to check an int value is containing another value (here 9) like this:

例如,这允许您检查一个 int 值是否包含另一个值(此处为 9),如下所示:

CONVERT(col1,char) LIKE '%9%'

回答by xdazz

You could use CONCAT, and the numeric argument of it is converted to its equivalent binary string form.

您可以使用CONCAT,并将其数字参数转换为其等效的二进制字符串形式。

select t2.* 
from t1 join t2 
on t2.url=CONCAT('site.com/path/%', t1.id, '%/more') where t1.id > 9000

回答by Bertzor

select t2.* 
from t1 join t2 on t2.url='site.com/path/%' + cast(t1.id as varchar)  + '%/more' 
where t1.id > 9000

Using concat like suggested is even better though

像建议一样使用 concat 会更好

回答by John Woo

Try it using CONCAT

尝试使用 CONCAT

CONCAT('site.com/path/','%', CAST(t1.id AS CHAR(25)), '%','/more')

回答by Mahmoud Gamal

You can do this:

你可以这样做:

select t2.*
from t1
join t2 on t2.url = 'site.com/path/' + CAST(t1.id AS VARCHAR(10)) + '/more' 
where t1.id > 9000

Pay attention to CAST(t1.id AS VARCHAR(10)).

注意CAST(t1.id AS VARCHAR(10)).