将MySQL表导出为csv文件

时间:2014-04-01 10:29:12

标签: mysql csv mysql-workbench

我有一个MySQL表,必须作为csv文件取出,我使用的查询是

SELECT "ID","NAME","SALARY","SAL1","SAL2","SAL3","SAL4","SAL5","SAL6","SAL7","SAL8","SAL9","SAL10","SAL11","SAL12","SAL13","SAL14","SAL15","SAL16","SAL17","SAL18","SAL19","SAL20","SAL21","SAL22","SAL23","SAL24","SAL25","SAL26"
UNION ALL
SELECT *
FROM addstock25
INTO OUTFILE "E:\\JOSE DATA\\addstock7.csv"
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

这个查询有效,如果我有200个列名怎么办? 没有手动输入它有没有办法做到这一点?

6 个答案:

答案 0 :(得分:51)

此命令几乎可以为您提供所需的功能,甚至可以与远程服务器配合使用。唯一需要注意的是它会生成TSV文件(字段由制表符分隔)。

mysql mydb -e "select * from mytable" -B > mytable.tsv 

但您可以使用sed将其转换为CSV,如this answer

中所述
mysql mydb -e "select * from mytable" -B | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > mytable.csv

答案 1 :(得分:4)

DESCRIBE addstock25;

剥去该列的第一列和前三个条目(取决于您的用法)。您将获得addstock25中的字段列表。

这将仅使用在核心...称为信息模式中派生的虚拟表来使用字段名称。

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='foo';

假设此查询的名称为sq_fieldnamelist

所以, 上面的表有一列,它的字段名称为“foo”表。

如果直接写这样

    SELECT (sq_fieldnamelist)
    UNION ALL
    SELECT *
    FROM addstock25
    INTO OUTFILE "E:\\JOSE DATA\\addstock7.csv"
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n

Mysql会出错。 “子查询返回多行”

我们必须编辑sq_fieldnamelist,以逗号分隔背靠背连接所有条目。

Select GROUP_CONCAT(COLUMN_NAME)
FROM
(SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='ffd_companies'
LIMIT 3,100
) AS fafa
GROUP BY 'COLUMN_NAME' // this group by is just to make group concat work

让我们说这是sq_fieldnamelist2

如果我们像这样编辑sq_fieldnamelist。它将只返回一个值,即使用逗号分隔的所有字段名称。所以现在我们可以将这个子查询放到你的select语句中来获取所需的字段。

SELECT (sq_fieldnamelist2)
UNION ALL
SELECT *
FROM addstock25
INTO OUTFILE "E:\\JOSE DATA\\addstock7.csv"
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n

您需要在sq_fieldnamelist2中编辑LIMIT 3,100以供您自己使用。

假设您的表格类似于fil1,fil2 ... filN,sal1,sal2,sal3 ....,salI以查看您应该使用的唯一工资字段LIMIT N,x>I+N。如果你想看到所有使用LIMIT 0,x>N+I

答案 2 :(得分:2)

我看不出你为什么不能做

SELECT *
FROM addstock25
INTO OUTFILE "E:\\JOSE DATA\\addstock7.csv"
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n

答案 3 :(得分:1)

我认为你正在寻找类似的东西。

    SET @sql = NULL;
        SELECT GROUP_CONCAT("'",COLUMN_NAME,"'")
          FROM
        (SELECT `COLUMN_NAME`
        FROM `INFORMATION_SCHEMA`.`COLUMNS` 
        WHERE `TABLE_SCHEMA` = 'yourdatabasename'
        and `TABLE_NAME`='ffd_companies'
        ) AS colnames
        GROUP BY 'COLUMN_NAME' 
        into @sql;

        SET @sql = concat ("SELECT", @sql, " from dual
        UNION ALL
        SELECT *
        FROM addstock25
        INTO OUTFILE 'E:\\JOSE DATA\\addstock7.csv'
        FIELDS TERMINATED BY ','
        ENCLOSED BY '", '"',"'
        )"
        );

        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;

希望这会对你有所帮助。

注意:我添加WHERE子句TABLE_SCHEMA ='yourdatabasename'

答案 4 :(得分:1)

我已经为简单的基于UI的代码集成了脚本。 在这里,您可以输入查询,只需获取包含所有行(包括列名称)的csv文件。

此脚本适用于Windows和Linux系统。

https://github.com/Bihari12/databasetocsv

这会将结果保存在代码所在的同一文件夹中的csv文件中。

答案 5 :(得分:0)

可以使用“表数据导出向导”,从那里可以复制选择的SQL;