MySQL导出到CSV - 字段包含"但不会被"

时间:2016-04-29 13:21:25

标签: mysql

我有一些表格,其中包含带双引号的字段,但它们是" inches"的名称或缩写的一部分,"不要逃离现场。

我需要将其导出为CSV文件,但MySQL不断破坏"并在CSV中创建额外的列。我怎么能处理"在一个字段值内?

代码:

select
   makers.maker_name,
   baits.bait,
   depth_line.depth,
   depth_line.lineout a
from makers
   inner join baits on makers.maker_id = baits.maker_id
   inner join depth_line on depth_line.bait_id = baits.bait_id
order by
   makers.maker_name asc,
   baits.bait,
   depth_line.depth asc
into outfile '/tmp/muskie.csv'
fields terminated by ',';

诱饵表中的样本数据:

1 个答案:

答案 0 :(得分:0)

我无法重现这个问题。

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.12    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `table0`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `table0` (
    ->   `column0` VARCHAR(255) DEFAULT NULL,
    ->   `column1` VARCHAR(255) DEFAULT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO `table0`
    ->   (`column0`, `column1`)
    -> VALUES
    ->   ('"inches"', 'inches'),
    ->   ('4"', 'Lure name, 8", char, char'),
    ->   ('"VALUE 1"', 'NEW VALUE 1');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT
    ->   `column0`,
    ->   `column1`
    -> FROM
    ->   `table0`
    -> INTO OUTFILE '/path/to/file/muskie.csv'
    -> FIELDS TERMINATED BY ',';
Query OK, 3 rows affected (0.00 sec)

档案:/path/to/file/muskie.csv

"inches",inches
4",Lure name\, 8"\, char\, char
"VALUE 1",NEW VALUE 1

<强>更新

添加参数OPTIONALLY ENCLOSED BY '"'

可能很有用
SELECT
  `column0`,
  `column1`
FROM
  `table0`
INTO OUTFILE '/path/to/file/muskie.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"';

档案:/path/to/file/muskie.csv

"\"inches\"","inches"
"4\"","Lure name, 8\", char, char"
"\"VALUE 1\"","NEW VALUE 1"