MySQL 用省略号截断文本

发布于 2024-11-16 01:23:36 字数 342 浏览 5 评论 0原文

假设我有一个 MySQL 表,其中有一列:“消息”。它是 TEXT 类型。 我现在想查询所有行,但文本可能很大(不是很大,但很大),我只想获取它们的摘要。例如,可以将结果填充到列表中。

有没有办法将文本修剪到特定长度(例如 10 个字符),并在文本被修剪时添加省略号?

例如:

Message
-----------
12345678901234
1234567890
12345
12345678901

查询结果:

1234567...
1234567890
12345
1234567...

谢谢!

Suppose I have a MySQL table of one column: "Message". It is of type TEXT.
I now want to query all rows, but the text can be large (not extremely large but large) and I only want to get a summary of them. For example the result can be populated into a list.

Is there a way to trim the text to a specific length (say, 10 characters), and add ellipsis if the text is trimmed?

For example:

Message
-----------
12345678901234
1234567890
12345
12345678901

Query result:

1234567...
1234567890
12345
1234567...

Thanks!

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

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

发布评论

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

评论(6

橘虞初梦 2024-11-23 01:23:36
select case when length(message) > 7 
then concat(substring(message, 1, 7), '...')
else message end as adapted_message
from ...

测试/确认:

SELECT CASE WHEN LENGTH('1234567890') > 7 
THEN CONCAT(SUBSTRING('1234567890', 1, 7), '...') 
ELSE '1234567890' END AS adapted_message

UNION  

SELECT CASE WHEN LENGTH('12345') > 7 
THEN CONCAT(SUBSTRING('12345', 1, 7), '...') 
ELSE '12345' END AS adapted_message
select case when length(message) > 7 
then concat(substring(message, 1, 7), '...')
else message end as adapted_message
from ...

to test/confirm:

SELECT CASE WHEN LENGTH('1234567890') > 7 
THEN CONCAT(SUBSTRING('1234567890', 1, 7), '...') 
ELSE '1234567890' END AS adapted_message

UNION  

SELECT CASE WHEN LENGTH('12345') > 7 
THEN CONCAT(SUBSTRING('12345', 1, 7), '...') 
ELSE '12345' END AS adapted_message
你又不是我 2024-11-23 01:23:36

这是一个简单的单行解决方案:

IF(CHAR_LENGTH(message) > 10, CONCAT(LEFT(message, 7),"..."), message)

Here's a simple one line solution:

IF(CHAR_LENGTH(message) > 10, CONCAT(LEFT(message, 7),"..."), message)
落花随流水 2024-11-23 01:23:36

或者...

SELECT CONCAT(LEFT(message, 7), IF(LENGTH(message)>7, "…", ""))
FROM table

or...

SELECT CONCAT(LEFT(message, 7), IF(LENGTH(message)>7, "…", ""))
FROM table
無處可尋 2024-11-23 01:23:36

您可以声明一个新的 ELLIPSIS 函数以使您的查询可读:

DELIMITER //

CREATE FUNCTION ELLIPSIS ( str TEXT, max_length INT )
RETURNS TEXT

BEGIN
   DECLARE str_out TEXT;

   IF LENGTH(str) <= max_length THEN
      SET str_out = str;
      
   ELSE
      SET str_out = CONCAT(SUBSTR(str, 1, max_length-3), '...');

   END IF;
   
   RETURN str_out;

END; //

DELIMITER ;

然后您只需执行以下操作:

SELECT ELLIPSIS(Message, 10);

You can declare a new ELLIPSIS function in order to make your query readable:

DELIMITER //

CREATE FUNCTION ELLIPSIS ( str TEXT, max_length INT )
RETURNS TEXT

BEGIN
   DECLARE str_out TEXT;

   IF LENGTH(str) <= max_length THEN
      SET str_out = str;
      
   ELSE
      SET str_out = CONCAT(SUBSTR(str, 1, max_length-3), '...');

   END IF;
   
   RETURN str_out;

END; //

DELIMITER ;

Then you simply do:

SELECT ELLIPSIS(Message, 10);
醉城メ夜风 2024-11-23 01:23:36

Have a look at the MySQL string functions, documented here. You should be able to use some combination of substring and concat to achieve your desired behaviour.

固执像三岁 2024-11-23 01:23:36

我的方法:

  • 设 x 为要显示的最大字符数(因此 x + 3 点将是显示的最长字符串)
  • 您总是希望 LEFT(field,x)
  • If LENGTH(field) > x + 3,追加 3 个点
  • 否则如果 LENGTH(field) > x,附加字段的剩余部分
SELECT CONCAT(
    LEFT(field,x),
    IF(LENGTH(field) > x+3,
       '...',
        IF(LENGTH(field) > x,
            MID(field,x+1,LENGTH(field)),
            ''
        )
    )
) FROM table

My approach:

  • Let x be the maximum number of characters to display (therefore x + 3 dots will be the longest string displayed)
  • You always want LEFT(field,x)
  • If LENGTH(field) > x + 3, append 3 dots
  • Otherwise if LENGTH(field) > x, append the remainder of field
SELECT CONCAT(
    LEFT(field,x),
    IF(LENGTH(field) > x+3,
       '...',
        IF(LENGTH(field) > x,
            MID(field,x+1,LENGTH(field)),
            ''
        )
    )
) FROM table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文