在mysql中导出数据的命令几乎都可以导出csv格式的数据文件,下面我来给大家举几个实例,希望些方法对各位有所帮助.
1.MySQL导出本地数据库数据到本地文件,Sql代码如下:
- load data infile '/tmp/test.csv'
- into table test_info
- fields terminated by ',' optionally enclosed by '"' escaped by '"'
- lines terminated by 'rn';
- //phpfensi.com
- load data infile '/tmp/test.csv'
- into table test_info
- fields terminated by ',' optionally enclosed by '"' escaped by '
- "'
lines terminated by 'rn';里面最关键的部分就是格式参数.
2.MySQL导出远程数据库数据到本地文件,代码如下:
mysql -A service_db -h your_host -utest -ptest -ss -e "SELECT * from t_apps limit 300;" | sed 's/t/","/g;s/^/"/;s/$/"/;s/n//g' > apps.csv
(sed部分可略,尤其是处理包含汉字的数据时).
3.mysqldump导出csv格式的数据文件,代码如下:
# 使用方法如下:mysqldump -uroot -ppassword -t -T/root testdb --fields-enclosed-by=" --fields-terminated-by=,
例:mysqldump -h your_host -utest -ptest -w "id<300" service_db t_apps > tt.sql
导出后格式如下:
"1","m11401","2013-06-22 23:00:01"
"2","m11402","2013-06-22 23:00:02"
"3","m11403","2013-06-22 23:00:03" |