MySQL表没有正确对齐

时间:2013-03-18 03:58:28

标签: mysql

我有一个与MySQL有关的问题。在这两张图片中可以看到问题:

http://imgur.com/NrOwSxS,yPo9Cra http://imgur.com/NrOwSxS,yPo9Cra#1

有谁知道为什么MySQL会这样做?它应该显示为一个漂亮而整洁的表,而不是这一堆胡言乱语。提前致谢! :d

3 个答案:

答案 0 :(得分:1)

首先,要表明没有什么是错的,请尝试以下查询:

SELECT firstname FROM contact_info

那应该看起来不错。现在试试这个:

SELECT firstname, lastname FROM contact_info

这就是你选择单个列的方法。

您真的想要将输出捕获到文件,此页面将向您展示如何:The MySQL Command-Line Tool

然后你可以学习使用其他程序来很好地格式化它。

答案 1 :(得分:0)

我假设您创建的表格有点像这样:

create table automobile (make char(10),model char(10),year int, color char(10), style char(50), MSRP int);
insert into automobile values ('Ford','Mustang',2006,'Blue','Convertible',27000);
insert into automobile values ('Toyota','Prius',2005,'Silver','Hybrid',22000);
insert into automobile values ('Toyota','Camry',2006,'Blue','Sedan',26000);
insert into automobile values ('Dodge','1500',2005,'Green','Pickup',26000);

所以

 describe automobile

会将您的列显示为:

+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| make  | char(10) | YES  |     | NULL    |       |
| model | char(10) | YES  |     | NULL    |       |
| year  | int(11)  | YES  |     | NULL    |       |
| color | char(10) | YES  |     | NULL    |       |
| style | char(50) | YES  |     | NULL    |       |
| MSRP  | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

只要您的列总数小于终端的宽度,您应该看到 预期的结果:

mysql> select * from automobile;
+--------+---------+------+--------+-------------+-------+
| make   | model   | year | color  | style       | MSRP  |
+--------+---------+------+--------+-------------+-------+
| Ford   | Mustang | 2006 | Blue   | Convertible | 27000 |
| Toyota | Prius   | 2005 | Silver | Hybrid      | 22000 |
| Toyota | Camry   | 2006 | Blue   | Sedan       | 26000 |
| Dodge  | 1500    | 2005 | Green  | Pickup      | 28000 |
+--------+---------+------+--------+-------------+-------+

如果您希望结果较小,请选择您想要查看的列。

 select make,model from automobile

mysql> select make,model from automobile;
+--------+---------+
| make   | model   |
+--------+---------+
| Ford   | Mustang |
| Toyota | Prius   |
| Toyota | Camry   |
| Dodge  | 1500    |
+--------+---------+

要使列的内容更小,您可以使用左字符串函数

select left(make,4) as make, left(model,5) as model,left(style,5) as style from automobile;
+------+-------+-------+
| make | model | style |
+------+-------+-------+
| Ford | Musta | Conve |
| Toyo | Prius | Hybri |
| Toyo | Camry | Sedan |
| Dodg | 1500  | Picku |
+------+-------+-------+

答案 2 :(得分:0)

您可以尝试在最后提供一个行分隔符。

mysql> LOAD DATA LOCAL INFILE *file_path* INTO *table_name* LINES TERMINATED BY '\r\n';

分隔符可能因编辑者而异。在Windows中,大多数编辑器都使用'\r\n'