将 mysql LONGTEXT 值转换为 VARCHAR 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9531109/
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
Convert mysql LONGTEXT value to VARCHAR value?
提问by Claes Gustavsson
I have a function that post on a users facebook wall.
我有一个在用户 facebook 墙上张贴的功能。
One thing that I send to facebook is some text that I get from my mysql table that is set to LONGTEXT. If I have the table set as LONGTEXT then the text is not send to facebook, but if I set the table to VARCHAR then the text is send to facebook!
我发送到 facebook 的一件事是我从设置为 LONGTEXT 的 mysql 表中获取的一些文本。如果我将表设置为 LONGTEXT 则文本不会发送到 facebook,但如果我将表设置为 VARCHAR 那么文本将发送到 facebook!
So how can I convert the LONGTEXT value that I get so it becomes like a VARCHAR value, before I send it to facebook?
那么在我将它发送到 facebook 之前,如何转换我得到的 LONGTEXT 值,使其变得像一个 VARCHAR 值?
I use the table in many other places so I just cant convert the table itself to VARCHAR, its to much work! I have to convert the output instead.
我在许多其他地方使用该表,所以我无法将表本身转换为 VARCHAR,它需要做很多工作!我必须转换输出。
Any input appreciated thanks!
感谢任何输入!
回答by Sudhir Bastakoti
In mysqlyou can do:
在mysql你可以这样做:
SELECT ID, CAST(YourLongText as char(255)) AS YourVarchar FROM some_table
Did you mean like that
你是那个意思吗
回答by dom
use cast as in the post from @Sudhir or use the mysql function convert:
使用来自@Sudhir 的帖子中的强制转换或使用 mysql 函数转换:
回答by Vitalii Maslianok
use the CONVERT or CAST functions: http://www.geeksengine.com/database/single-row-functions/conversion-functions.php
使用 CONVERT 或 CAST 函数:http: //www.geeksengine.com/database/single-row-functions/conversion-functions.php
CONVERT(expr,type) In this form, CONVERT takes a value in the form of expr and converts it to a value of type.
CONVERT(expr,type) 在这种形式中,CONVERT 采用 expr 形式的值并将其转换为类型的值。
CAST(expr AS type) Using CAST() function is the same as using CONVERT() function except that it uses keyword AS in between expr and type rather than a comma.
CAST(expr AS type) 使用 CAST() 函数与使用 CONVERT() 函数相同,只是它在 expr 和 type 之间使用关键字 AS 而不是逗号。