简体   繁体   English

如何将整个数据库MYSQL导出到CSV文件?

[英]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.

相关问题 如何将 mysql 表导出到 csv 文件并下载 - How can i export mysql table to csv file and download it PHP。 将下载的csv传输到mysql数据库-如何在PHP内存中存储下载的csv文件 - PHP. Transferring downloaded csv to mysql database - how can I store downloaded csv file in php memory 我在mysql数据库中有一些记录。我想使用php以csv文件格式导出每日记录? - I have some record in mysql database .I want to export daily record in csv file format using php? 我如何在没有cpanel访问的情况下导出mysql数据库 - How can I export mysql database without cpanel access 如何将csv文件中的数据插入mySQL数据库(大数据,快速方式)? - How can I insert data from csv file into mySQL database (big data,fast way)? PHP:如何将CSV文件的内容逐行获取到MySQL数据库中? - PHP: How can I get the contents of a CSV file into a MySQL database row by row? 如何使用 PHP 更高效地将 csv 文件导入 MySQL 数据库? - How can I import csv file to MySQL database more efficiently with PHP? 使用php从mysql数据库导出csv文件 - export a csv file from the mysql database using php 为什么我不能在此 Codeigniter 应用程序中将 MySQL 表值导出为 CSV 文件? - Why can't I export MySQL table values as a CSV file in this Codeigniter application? MySQL PHP CSV文件导出 - MySQL PHP CSV File Export
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM