一発で出来て便利な方法を見つけたので、書いておきます。
ただ、入力の場合、フィールド数は同じである必要があります。
【出力】
書式
SELECT * FROM [TABLE] INTO OUTFILE "[ファイルのパス]" FIELDS TERMINATED BY "[区切り文字]" OPTIONALLY ENCLOSED BY '[囲み文字]' LINES TERMINATED BY "[改行コード]";
MySQLで実行する場合
SELECT * FROM [TABLE] INTO OUTFILE "[ファイルのパス]" FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY "\r\n";
PHPで実行する場合(SQL文以外省略しています)
select * from [TABLE] into outfile \"[ファイルのパス]\" fields terminated by \",\" optionally enclosed by '\"' lines terminated by \"\r\n\"
【入力】
load data infile \"[ファイルのパス]\" into table [テーブル名] fields terminated by \"[区切り文字]\" enclosed by '[囲み文字]' lines terminated by \"[改行コード]\"
MySQLで実行する場合
LOAD DATA INFILE "/tmp/temporary.csv" INTO TABLE TABLE FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY "\r\n";
PHPで実行する場合(SQL文以外省略しています)
load data infile \"/tmp/temporary.csv\" into table TABLE fields terminated by \",\" enclosed by '\"' lines terminated by \"\r\n\"