12

如何导出仅指定某些字段的表?

我正在使用 mysql 5.0 - 使用 sql 命令或 mysqldump。

我的表是 X,我要导出的字段是 A,B,C

4

5 回答 5

21
SELECT A,B,C
FROM X
INTO OUTFILE 'file name';

您需要 FILE 权限才能执行此操作,并且它不会覆盖文件。

INTO OUTFILE它也有很多选项,例如FIELDS ENCLOSED BY,FIELDS ESCAPED BY等...您可能想在手册中查找

要生成 CSV 文件,您可以执行以下操作:

SELECT A,B,C
INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM X;

要从文件中重新加载数据,请使用该LOAD DATA INFILE命令与您用于转储它的选项相同。对于上面的 CSV 格式,那将是

LOAD DATA INFILE '/tmp/result.txt'
INTO TABLE X
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
于 2008-12-12T15:31:37.020 回答
8

If you are using phpMyAdmin,

Run the query

SELECT A,B,C FROM X

and there is an export option in the bottom of the result.

于 2013-10-20T06:44:13.733 回答
5

OUTFILE不会为您生成 SQL 转储,也mysqldump不能对列的子集进行操作。但是您可以create table temp_weeeee select ....导出并删除它。

于 2012-07-18T20:49:10.883 回答
4

外壳命令

echo 'select field from db.table;' | mysql -u user -p password > output.txt
于 2013-04-16T09:10:21.173 回答
1

尝试:

SELECT col1, col2
  INTO OUTFILE '/filepath/export.txt'
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
FROM table;
于 2008-12-12T15:35:06.170 回答