Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将MySQL查询的结果写入循环中的文本文件?_Mysql_Loops - Fatal编程技术网

如何将MySQL查询的结果写入循环中的文本文件?

如何将MySQL查询的结果写入循环中的文本文件?,mysql,loops,Mysql,Loops,我有一个庞大的个人数据库(比如说每个人的1000personals/5000记录)。我想为每个人(在这个例子中,让我们说lat和long)写几个字段到一个文本文件(最好用逗号分隔) 算法如下所示: foo=select distinct (id) from <table-name>; for each id in foo { smaller_result= select lat,long from <table-name> where id=$id; write smal

我有一个庞大的个人数据库(比如说每个人的
1000
personals/
5000
记录)。我想为每个人(在这个例子中,让我们说lat和long)写几个字段到一个文本文件(最好用逗号分隔)

算法如下所示:

foo=select distinct (id) from <table-name>;
for each id in foo
{
smaller_result= select lat,long from <table-name> where id=$id;
write smaller_result to a text file with unique name (e.g. id.txt);
}
foo=从中选择不同的(id);
对于foo中的每个id
{
较小的_结果=选择lat,长距离,其中id=$id;
将较小的结果写入具有唯一名称的文本文件(例如id.txt);
}
我可以很容易地用PHP编写代码(如果我不能直接运行命令行SQL查询,我经常使用PHP与MySQL接口)。但是,在这种情况下,我需要与合作者共享代码,并让他运行代码(他没有也不能安装php)。此外,数据库相当大,无法轻松在线上传(这将允许我通过web运行查询)。那么,除此之外,我将如何实现这一点呢

a) 该算法是否可以编写为可以在命令行中执行的sql查询

b) 如果不是,是否可以用python编写,这样我的合作者就可以运行一个.py文件


我们都在OSX(Lion)上,可以从shell/终端访问mysql和python。

您可以使用select输出任何select查询的结果。。。进入外文件。这将生成带有输出的文本文件(在服务器上)


因此,您需要创建一个SELECT查询来生成文件。。。我认为在您的情况下,使用子查询可以很容易地完成,如果没有,您可以使用游标创建一个存储过程,该游标循环“foo”结果集并将其附加到临时表,然后使用SELECT。。。将该表放入OUTFILE。

Hmmm…上次我用PHP检查了Lion ships,所以他应该已经安装了它。