我需要从mysql数据库中获取数据。我有以下类似的东西:
set @filename=concat('/home/reports/',current_timestamp(),'.csv');
set @querys=concat("select * from tablename limit 10 ",
"into outfile ? fields terminated by ',';");
prepare s1 from @querys;
execute s1 using @filename;
deallocate prepare s1;
并且我收到此错误:错误代码:1064。您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获取在'?附近使用的正确语法。第1行以',''结尾的字段你知道为什么吗?
我非常怀疑MySQL是否支持以insert into outfile
语法将查询字符串作为参数传递。您将需要进行字符串连接。引用文件名也是一个好主意,因为它将包含空白字符。
set @filename = concat('/home/reports/', current_timestamp(), '.csv');
set @query=
concat(
"insert into outfile '",
@filename,
"' select * from tablename limit 10 ",
"fields terminated by ',';"
);
prepare s1 from @query;
execute s1;
deallocate prepare s1;