2013-03-27 44 views
-2

如何只选定的文件在mysql中导出到.csv文件如何只选定的文件在mysql中导出到.csv

例如:

Select * from tablename where column name = $somevalue 

如何返回值从查询导出?

+0

这个问题并没有表现出任何的研究工作。 **做你的作业很重要**。告诉我们你发现了什么,***为什么它不符合你的需求。这表明你已经花时间去尝试帮助你自己了,它使我们避免重申明显的答案,最重要的是它可以帮助你得到更具体和相关的答案。 [FAQ](http://stackoverflow.com/questions/how-to-ask)。 – 2013-03-27 19:31:52

+0

对不起,我甚至不知道从哪里开始。 – 2013-03-27 19:35:38

回答

0

使用into outfile(文档here):

select * 
into outfile 'the/file/you/want.csv' 
from tablename 
where column = $somevalue 

哦,添加你需要使用union all列名。 Yuch,因为这可能需要您将某些列明确地转换为正确的格式。

select <all columns except "isheader"> 
into outfile 'the/file/you/want.csv' 
from ((select 1 as isheader, <list of column names in quotes> 
    ) union all 
     (select 0 as isheader, t.* 
     from tablename 
     where column = $somevalue 
    ) 
    ) t 
order by isheader desc 

举例来说,如果你有一列名为id

select id 
into outfile 'the/file/you/want.csv' 
from ((select 1 as isheader, 'id' as id 
    ) union all 
     (select 0 as isheader, t.id 
     from tablename t 
     where column = $somevalue 
    ) 
    ) t 
order by isheader desc 
+0

谢谢队友。你很聪明 – 2013-03-27 19:36:07

+0

我应该在phpmyadmin中写这段代码吗? – 2013-03-27 19:37:23

+0

队友如何将标题或标题放在列中?我设法把脱钩者已经 – 2013-03-27 19:44:05