在 MySQL 命令中将列限制为特定宽度

ant*_*tak 7 mysql

mysql命令提供了很好的表格输出,其中考虑了半宽和全宽间距。

例如:

> select 'The quick brown fox jumps over the' text union select '???????????';
+------------------------------------+
| text                               |
+------------------------------------+
| The quick brown fox jumps over the |
| ???????????             |
+------------------------------------+
2 rows in set
Run Code Online (Sandbox Code Playgroud)

鉴于此,如何将列截断为特定宽度?

例如,我如何得到这个?

+----------------+
| text           |
+----------------+
| The quick brow |
| ??????? |
+----------------+
Run Code Online (Sandbox Code Playgroud)

编辑:就我尝试过的而言。

如果我通过将查询更改为使用以下内容来尝试此操作LEFT()

> select left(text, 7) text from (select 'The quick brown fox jumps over the' text union select '???????????') x;
+-----------------------+
| text                  |
+-----------------------+
| The qui               |
| ???????        |
+-----------------------+
2 rows in set
Run Code Online (Sandbox Code Playgroud)

我得到的并不完全是我想要的。我不希望每个字符串有 7 个左字符,我希望两个字符串都显示尽可能多的字符以最佳地适应给定的宽度。我想要一个类似于“例如我如何得到这个?”下的结果,如果说这个宽度是 14(半角字符)。

MySQL 确实可以处理不同的宽度,正如第一个示例所建立的那样。(用等宽编辑器查看它,管道排成一行。)任何解决方案,如 SQL Plus' column foo format a10,或使用ENCODE()and BIT_LENGTH(),或用户函数都可以工作。缺乏答案可能意味着没有简单的解决方案?

小智 0

试试这个...

select substring('The quick brown fox jumps over the lazy dog',1,14 )text 
union select substring('0 1 2 3 4 5 6 7 8 9 0',1,13) ;
Run Code Online (Sandbox Code Playgroud)