如何导出仅指定某些字段的表?
我正在使用 mysql 5.0 - 使用 sql 命令或 mysqldump。
我的表是 X,我要导出的字段是 A,B,C
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';
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.
OUTFILE
不会为您生成 SQL 转储,也mysqldump
不能对列的子集进行操作。但是您可以create table temp_weeeee select ....
导出并删除它。
外壳命令
echo 'select field from db.table;' | mysql -u user -p password > output.txt
尝试:
SELECT col1, col2
INTO OUTFILE '/filepath/export.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM table;