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

发布于 2025-01-05 01:34:27 字数 251 浏览 3 评论 0原文

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

从 blog_entry 中选择*;

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话难免假 2025-01-12 01:34:27

使用 MySQL 的 SUBSTRING 函数,如文档中所述< /a>. Like:

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

选择前 100 个字符。

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

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

To select first 100 chars.

拧巴小姐 2025-01-12 01:34:27

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

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

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

SELECT LEFT(LongField, 20) AS LongField_First20chars
FROM ...
深空失忆 2025-01-12 01:34:27

清理终端窗口中查询结果的可读性的最佳方法是使用 mysql 分页器,而不是修改查询,因为这可能太麻烦了。

  1. 设置寻呼机:

    <代码>mysql> pager less -S

  2. 执行您的查询:

    <代码>mysql> SELECT * FROM ...

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

mysql> pager more

如果需要,只需运行即可返回正常的输出流。

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.

  1. Set the pager:

    mysql> pager less -S

  2. Do your query:

    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 Q to get out of pager mode for that query, and then just run

mysql> pager more

to return to the normal output river if you want.

缘字诀 2025-01-12 01:34:27
Select Cast(theLongTextField As VarChar(100)) From blogEntry
Select Cast(theLongTextField As VarChar(100)) From blogEntry
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文