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

时间:2013-03-27 19:31:06

标签: mysql sql

如何仅将mysql中的选定文件导出为.csv文件

示例:

Select * from tablename where column name = $somevalue

如何从该查询中导出返回的值?

1 个答案:

答案 0 :(得分: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