如何缩进mysql SELECT INTO OUTFILE的结果

发布于 2024-11-15 07:24:41 字数 231 浏览 3 评论 0原文

我有一个 sql 表,我想将其转储到输出文件。问题是,有些结果是 1 位数字,即 1 或 2,而其他结果是长数字,即 1.2323523

这会导致输出文件看起来很糟糕,类似于这个

1   a bbb
1.21321342 aaaa bbbbb 

意思,每列的开头未对齐。有谁知道sql中有没有办法解决这个问题?

谢谢!!!

I have an sql table that I want to dump to an outfile. The thing is, some of the results are 1 digit, i.e 1 or 2, while others are long numbers, i.e. 1.2323523

This causes the outfile to look terrible, something like this

1   a bbb
1.21321342 aaaa bbbbb 

meaning, the beginning of each column is not aligned. Does anyone know if there is a way in sql to take care of that?

Thanks!!!

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

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

发布评论

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

评论(3

疏忽 2024-11-22 07:24:41

将列转换为 char(n),例如

select cast(my_numeric_column as char(10)) as my_numeric_column

这将输出固定宽度的列数据。然而,它们将被保留。

更好的解决方案是为所有数字提供相同的小数位数,1 将输出为“1.000000”等。具体做法如下:

select cast(my_numeric_column as decimal(16,8)) as my_numeric_column

选择所需的精度(位数 - 此处为 8)。

所有列都可以以这种方式处理,以生成良好的表格格式。

cast the columns as char(n), eg

select cast(my_numeric_column as char(10)) as my_numeric_column

This will be output fixed-width column data. They will however be left justified.

A better solution would be to give the same number of decimal places to all numbers, 1 would be output as "1.000000" etc. This is how to do that:

select cast(my_numeric_column as decimal(16,8)) as my_numeric_column

Chose the precision (number of places - here 8) you want.

All columns can be treated in this way to produce a nicely tabulated format.

上课铃就是安魂曲 2024-11-22 07:24:41

像这样使用你会得到一个csv文件,所以在excel中它看起来很漂亮

SELECT * 
FROM mytable
INTO OUTFILE '/tmp/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

use like this u get a csv file, so in excel its look pretty

SELECT * 
FROM mytable
INTO OUTFILE '/tmp/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
花落人断肠 2024-11-22 07:24:41

您可以使用 mysql 中的字符串操作函数之一 。值得注意的是, LPAD格式

You can use one of the string manipulation functions in mysql. Notably, LPAD or FORMAT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文