MySQL 限制 SELECT 结果中长文本字段的长度

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

Limit length of longtext field in SELECT results

mysqlsqllongtext

提问by maxm

I'm executing a SELECT query on a table in MySQL using the command-line interface (not a GUI client):

我正在使用命令行界面(不是 GUI 客户端)对 MySQL 中的表执行 SELECT 查询:

SELECT * FROM blog_entry;

SELECT * FROM blog_entry;

One of blog_entry's fields is of type 'longtext' and is such a long piece of text that when the result is displayed in my terminal the display of rows takes more than one line. This causes an ugly mess of a display, where columns aren't easily visible. What technique can I use in my SELECT query that would limit the number of characters displayed for each field so that the printed row results don't overflow to new lines?

blog_entry 的字段之一是“longtext”类型,是一段很长的文本,当结果显示在我的终端中时,行的显示需要多于一行。这会导致显示一团糟,其中列不容易看到。我可以在 SELECT 查询中使用什么技术来限制每个字段显示的字符数,以便打印的行结果不会溢出到新行?

回答by Oldskool

Use MySQL's SUBSTRINGfunction, as described in the documentation. Like:

使用 MySQL 的SUBSTRING功能,如文档中所述。喜欢:

SELECT SUBSTRING(`text`, 1, 100) FROM blog_entry;

To select first 100 chars.

选择前 100 个字符。

回答by ypercube??

You can use the LEFT()function to get only the first characters:

您可以使用该LEFT()函数仅获取第一个字符:

SELECT LEFT(LongField, 20) AS LongField_First20chars
FROM ...

回答by AgmLauncher

The best way to clean up the readability of the results from a query in your terminal window is to use the mysql pager, not modifying your query as that can be too cumbersome.

清除终端窗口中查询结果可读性的最佳方法是使用 mysql 寻呼机,而不是修改查询,因为这可能太麻烦。

  1. Set the pager:

    mysql> pager less -S

  2. Do your query:

    mysql> SELECT * FROM ...

  1. 设置寻呼机:

    mysql> pager less -S

  2. 做你的查询:

    mysql> SELECT * FROM ...

This will put your results in a more readable format. You can use your arrow keys to page up and down and left and right to see the full table. Just press Qto get out of pager mode for that query, and then just run

这将使您的结果更具可读性。您可以使用箭头键上下左右翻页以查看完整表格。只需按下Q即可退出该查询的寻呼模式,然后运行

mysql> pager more

to return to the normal output river if you want.

如果需要,可以返回正常输出河流。

回答by Bassam Mehanni

Select Cast(theLongTextField As VarChar(100)) From blogEntry