如何仅将 mysql 中的选定文件导出到 .csv 文件
例子:
Select * from tablename where column name = $somevalue
如何从该查询中导出返回值?
使用into outfile
(此处的文档):
select *
into outfile 'the/file/you/want.csv'
from tablename
where column = $somevalue
哦,要添加列名,您需要使用union all
. 太好了,因为这可能需要您将某些列显式转换为正确的格式。
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