我试图将表的某些字段导出到csv文件中... 我正在使用的查询是
SELECT "field1", "field1", "field3"
UNION ALL
SELECT *
FROM mytable
INTO OUTFILE "/Users/davide/Documents/file.csv"
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
但mysql返回
ERROR 1222 (21000): The used SELECT statements have a different number of columns
桌上有2个字段,但我不知道'认为这是问题。
由于
答案 0 :(得分:2)
请勿使用SELECT *
,列出要放入CSV的特定列。 SELECT *
表示返回表格中的所有列。
SELECT "field1", "field1", "field3"
UNION ALL
SELECT field1, field2, field3
FROM mytable
INTO OUTFILE "/Users/davide/Documents/file.csv"
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
答案 1 :(得分:0)
问题在于,当您使用UNION
语句时,必须确保SELECT
个语句具有相同的列数
如果您需要填写“空”空格,可以使用NULL
,例如
SELECT "field1", "field1", "field3", NULL, NULL
UNION ALL
SELECT *
FROM mytable
INTO OUTFILE "/Users/davide/Documents/file.csv"
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'