MySQL部分数据导出:简单易行的方法(mysql部分数据导出)
MySQL是一个非常流行的数据库,也是许多开发人员最熟悉的数据库。有时要从MySQL查询数据并将结果集以外部文件格式导出,以便在本地进行查看和分析。本文将介绍如何简单易行地将MySQL数据导出。
1.最简单的方法是使用MySQL客户端程序(如MySQL Workbench)对查询结果集进行“复制/粘贴”操作,将结果集的内容复制到文本编辑器中,再将其保存到本地硬盘;
2.使用MySQL客户端程序或命令行模式以及SELECT INTO OUTFILE子句,可以实现;
MySQL> SELECT * FROM table1 INTO OUTFILE ‘/tmp/table1.txt’
3.在MySQL Query Browser中,选择要导出的查询结果,单击“工具”菜单,从下拉菜单中选择“导出”,然后选择导出文件的文件格式;
4.在MySQL Workbench中,打开“对象”,从左边树形菜单中双击所要导出的表,然后点击工具栏上的“导出数据”按钮,然后根据提示设置相关的参数;
以上是MySQL部分数据导出的几种简单易行的方法,比较适用于处理简单的数据导出任务,但是对于一些较复杂的或大量的数据导出任务,可能要使用相关的数据库技术,或使用各种编程语言,以下是一个简单的PHP脚本,以实现MySQL数据导出:
$link = mysql_connect(“localhost”, “username”, “password”);
$result = mysql_query(‘SELECT * FROM table1’, $link);
$string=”id\tname\tAcr\r
“;
while($row = mysql_fetch_row($result)) {
$line=”;
foreach($row as $value) {
if ((!isset($value)) OR ($value == “”)) {
$value = “\t”;
} else {
# important to escape any quotes to preserve them in the data.
$value = str_replace(‘”‘, ‘””‘, $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = ‘”‘.$value.'”‘.”\t”;
}
$line .= $value;
}
$string.=trim($line).”\r
“;
}
$string = str_replace(“\r
“,”\r
“,$string);
# This line will stream the file to the user rather than spray it across the screen
header(“Content-type: application/octet-stream”);
Header(“Content-Disposition: attachment; filename=table1.xls”);
#echo “id\tname\tAcr\r
“;
echo $string;
?>