2009年1月21日水曜日

複数回のselectを実行した結果をファイルに出力する

select col1,col2 ... col10 from table where col11 = 'hoge1' and col12 = 'hoge2' and col13 = 'hoge3';
select col1,col2 ... col10 from table where col11 = 'hoge4' and col12 = 'hoge5' and col13 = 'hoge6';
select col1,col2 ... col10 from table where col11 = 'hoge7' and col12 = 'hoge8' and col13 = 'hoge9';

こんな感じで、2000件超のselect文を実行してその結果をファイルに出力する、というのをやりました。
↓やり方
1.2000件超のselect文をファイルに保存します。名前はselect2000.txtとします。
2.select2000.txをサーバーにアップ。
3.アップしたディレクトリに移動して、以下を実行。
$ mysql DB_NAME --skip-column-names < select2000.txt > output2000.txt
これでoutput2000.txtにselectの実行結果がタブ区切りで保存されます。「--skip-column-names」を入れないと、1行ごとにカラム名が入ってしまいます。実行結果が2000行だとカラム名も2000行になるので合計4000行です。
他にもいくつかオプションがあります。必要に応じて使い分けると効率が上がると思います。
参考にさせてもらったページ
http://f32.aaa.livedoor.jp/~azusa/index.php?t=mysql&p=file

0 件のコメント:

コメントを投稿