我想将使用多个联接、分组依据和排序依据的查询结果输出到 CSV 文件中。
查询本身使用 UNION 生成结果集、一个空白行和一个总计行。
当我尝试使用 UNION ALL 将文件输出到 CSV 时,出现错误
select "header1", "header2", "header3"
UNION ALL
(
select field1, field2, field3
from tablename1
UNION
select "","",""
UNION
select "Total", field2, field3
)
into outfile 'c:\\data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Err 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (select "","","","","","","","","","","" ) ' at line <6>
预先感谢您的帮助。
最佳答案
请注意这样的事情:
select 1, 2, 3
union all
(
select 0,4,6
union
select 1,1,1
)
不会工作。
您应该使用像这样的派生表:
select 1, 2, 3
union all
select * from (
select 0,4,6
union
select 1,1,1
) x
关于mysql - 将 mysql 查询的结果(带有多个 UNION)输出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55899311/