如何截断为 MySQL 查询中的列返回的文本

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

How to truncate the text returned for a column in a MySQL query

sqlmysql

提问by morpheous

I have a table in a MySQL database that I am running simple SELECT queries on (for quick diagnostics/analysis - since I am not running phpmyadmin on the server - for security reasons).

我在 MySQL 数据库中有一个表,我正在运行简单的 SELECT 查询(用于快速诊断/分析 - 因为我没有在服务器上运行 phpmyadmin - 出于安全原因)。

I would like to be able to truncate the returned data using something like this:

我希望能够使用这样的东西截断返回的数据:

select id, LEFT(full_name, 32), age FROM user

where user is a table that contains the columns id, full_name and age

其中 user 是一个包含列 id、full_name 和 age 的表

I tried the above statement and it didn't work. anyone knows how to do this?

我尝试了上面的语句,但没有奏效。有谁知道怎么做?

[Edit]

[编辑]

Sorry, when I said it dosen't work, I mean mySQL simply returns the STRING "LEFT(full_name, 32)" as an alias for the column 'full_name' and outputs the field value - which in this case, can be as long as 256 chars.

抱歉,当我说它不起作用时,我的意思是 mySQL 只是返回字符串“LEFT(full_name, 32)”作为列“full_name”的别名并输出字段值 - 在这种情况下,可以是一样长作为 256 个字符。

回答by mvds

select id, SUBSTRING(full_name,1, 32), age FROM user

Quoting mysql.com:

引用mysql.com

For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

对于所有形式的SUBSTRING(),将要从中提取子串的字符串中第一个字符的位置计算为 1。

回答by Fosco

select id, SUBSTRING(full_name,1, 32), age FROM user 

回答by kheengz

select id, SUBSTR(full_name, 1, 32), age FROM user;

OR

或者

select id, SUBSTRING(full_name, 1, 32), age FROM user;

OR

或者

select id, SUBSTRING(full_name, FROM 1 FOR 32), age FROM user

Note: SUBSTR()is a synonym for SUBSTRING().

注意SUBSTR()是 的同义词SUBSTRING()

found on mysql doc

在 mysql 文档中找到