[英]How can I export into an CSV file a whole database MYSQL?
I have a DB named demo_db with the following tables: 我有一个名为demo_db的数据库, 其中包含以下表格:
user
Id
Name
history
Id
userId
report
start_test
Id
userId
score
final_test
Id
userId
score
How can I export all the tables into a CSV file using INTO OUTFILE in MySQL? 如何在MySQL中使用INTO OUTFILE将所有表导出为CSV文件? So I can have the column name and below the data, i want to be a able to add filters, if there's maybe a way to add each table to a new tab in the same file....or if I can just display all the data in a single line for each user??
所以我可以拥有列名和数据下方,我希望能够添加过滤器,如果有可能的方法将每个表添加到同一文件中的新选项卡....或者我可以只显示所有每个用户的单行数据??
Can't I do something like: SELECT user. 我不能做类似的事情:SELECT用户。 , history.
,历史。 , start_test.*, final_test.* INTO OUTFILE FROM user, history, start_test, final_test
,start_test。*,final_test。* INTO OUTFILE FROM user,history,start_test,final_test
I did it with mysqldump but it doesnt present the data as I want. 我用mysqldump做了它,但它没有按我想要的方式呈现数据。
I'm doing this with PHP. 我是用PHP做的。
There isn't a command to do this directly, you'll need to emulate the behavior of mysqldump and output the CSV the way you want it. 没有命令直接执行此操作,您需要模拟mysqldump的行为并以您希望的方式输出CSV。
$FILE = fopen("output.csv", "w");
mysql_connect($server, $login, $password);
$res = mysql_query("SHOW TABLES FROM $db");
$tables = array();
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$tables[] = "$row[0]";
}
foreach($tables as $table) {
$columns = array();
$res = mysql_query("SHOW COLUMNS FROM $table");
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$columns[] = "$row[0]";
}
fwrite($FILE, implode(",", $columns); fwrite("\n");
$resTable = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($resTable, MYSQL_NUM)) {
fwrite($FILE, implode(",", $row)); fwrite("\n");
}
}
MYSQL_NUM is used to give you a numeric indexed array. MYSQL_NUM用于为您提供数字索引数组。
If you don't want to output to a file, but rather directly download it from a website to your computer, add the following lines to the beginning of the script: 如果您不想输出到文件,而是直接从网站下载到您的计算机,请将以下行添加到脚本的开头:
header('Content-Type: text/csv; name="filename.csv"');
header('Content-Disposition: attachment; filename="filename.csv"');
Change the fwrites to echo or print. 将fwrites更改为echo或print。
Oh, and I added some newlines to the output since I'm sure you wouldn't want one big long line. 哦,我在输出中添加了一些新行,因为我相信你不会想要一个很长的行。
If you are using phpmyadmin 如果您使用的是phpmyadmin
Then Go to Export -> Click on Custom - display all possible options
然后转到导出 - >单击自定义 -
display all possible options
below you will find checkbox: Put columns names in the first row
. 您将在下面找到复选框:
Put columns names in the first row
。 Check that to get column names in the first row and below you will get data. 检查是否在第一行和下面获取列名称,您将获得数据。
CREATE VIEW MY_VIEW
AS
SELECT user.*, history.*, start_test.*, final_test.*
FROM
user, history, start_test, final_test
WHERE user.id = history.userId
AND user.id = start.userId
AND user.id = final.userId
Dump the content of the view use export tools in phpmyadmin 转储视图的内容使用phpmyadmin中的导出工具
This snippet taken from http://dev.mysql.com/doc/refman/5.6/en/select-into.html 此片段取自http://dev.mysql.com/doc/refman/5.6/en/select-into.html
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.