MySQL 如何对齐右调整列

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

How to align a column right-adjusted

mysqlsqlselectformattabular

提问by BlueFox

I want to align a column in my table. In this case 'Title'

我想对齐表格中的一列。在这种情况下“标题”

mysql> SELECT Titel, KuenstlerName from Bild;
+--------------------------+---------------+
| Title                    |         Artist|
+--------------------------+---------------+
| Drei Musikanten          | Picasso       |
| Buveuse assoupie         | Picasso       |
| Die Fl?te des Pan        | Picasso       |
| Paolo als Harlekin       | Picasso       |
| Die Umarmung             | Picasso       |
| Sitzende Frau            | Picasso       |
| Sternennacht             | van Gogh      |
| Der Park                 | Klingt        |
| Liegender Hund im Schnee | Marc          |
| Hauptweg und Nebenwege   | Klee          |
| Jungfrau im Baum         | Klee          |
| Das gelbe Haus           | Klee          |
+--------------------------+---------------+

Is it possible to align the left column to the right, like this:

是否可以将左列向右对齐,如下所示:

+--------------------------+---------------+
| Title                    | Artist        |
+--------------------------+---------------+
|           Drei Musikanten| Picasso       |
|          Buveuse assoupie| Picasso       |
|         Die Fl?te des Pan| Picasso       |
+--------------------------+---------------+

Thanks for your help!

谢谢你的帮助!

回答by Sadikhasan

If your Titlelength is 150 then query like this

如果您的Title长度为 150,则像这样查询

 SELECT LPAD(Titel,150,' ') as Titel , KuenstlerName from Bild;

Check Manualfor LPADfunction.

检查手册LPAD功能。

回答by gbn

If Titelis 100 characters, then do this

如果Titel是 100 个字符,则执行此操作

SELECT
    LPAD(Titel, 100, ' '),
    ...

or

或者

SELECT
    RIGHT(CONCAT(REPEAT(' ', 100), Titel), 100),
    ...

回答by Jango JR

I solved the task like this:

我解决了这样的任务:

SELECT LPAD(Titel,(SELECT MAX(LENGTH(Titel)) FROM Bild),' ') AS Titel, CONCAT(Kuenstler.Vorname,' ',Kuenstler.Name) AS Kuenstler 
FROM Kuenstler 
INNER JOIN Bild ON Bild.Kuenstler = Kuenstler.KID;