Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

Trying to achive query result into csv file with the below query--

SET @outpath = '/var/lib/mysql-files/';
SET @outfile = date_format(curdate(),'%Y%m%d');
SET @outextension = concat(@outpath,'StepCount_',@outfile,'.csv ');
SET @SQL :=Concat("select 'Participent Name','Email ID','DAY1','DAY2','DAY3','DAY4','DAY5','DAY6','DAY7','DAY8','DAY9','DAY10','DAY11','DAY12','DAY13','DAY14','DAY15','DAY16','DAY17','DAY18','DAY19','DAY20','DAY21','DAY22','DAY23','DAY24','DAY25','DAY26','DAY27','DAY28'
union all
select concat(ifnull(u.First_name,''),' ',ifnull(u.Middle_name,''),' ',ifnull(u.last_Name,'')) Name,u.email AS 'Email ID',ifnull(s.day_4,'') AS Day1,ifnull(s.day_5,'') AS Day2,ifnull(s.day_6,'') AS Day3,ifnull(s.day_7,'') AS Day4,ifnull(s.day_8,'') AS Day5,ifnull(s.day_9,'') AS Day6,ifnull(s.day_10,'') AS Day7,ifnull(s.day_11,'') AS Day8,ifnull(s.day_12,'') AS Day9,ifnull(s.day_13,'') AS Day10,ifnull(s.day_14,'') AS Day11,ifnull(s.day_15,'') AS Day12,ifnull(s.day_16,'') AS Day13,ifnull(s.day_17,'') AS Day14,ifnull(s.day_18,'') AS Day15,ifnull(s.day_19,'') AS Day16,ifnull(s.day_20,'') AS Day17,ifnull(s.day_21,'') AS Day18,ifnull(s.day_22,'') AS Day19,ifnull(s.day_23,'') AS Day20,ifnull(s.day_24,'') AS Day21,ifnull(s.day_25,'') AS Day22,ifnull(s.day_26,'') AS Day23,ifnull(s.day_27,'') AS Day24,ifnull(s.day_28,'') AS Day25,ifnull(s.day_29,'') AS Day26,ifnull(s.day_30,'') AS Day27,ifnull(s.day_31,'') AS Day28  from users u Left join step s on u.id=s.fk_user_id  ",CHAR(39),@outextension,CHAR(39)) ;
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;


but getting below error --

Quote:
Error Code: 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 ''/var/lib/mysql-files/StepCount_20220117.csv '' at line 3


What I have tried:

Quote:
Remove the double commas , insert space between users and file name etc


but still getting same error.
Posted
Updated 16-Jan-22 23:33pm

1 solution

Take a look at your final query:
SQL
SELECT ...
UNION ALL SELECT ...
'/var/lib/mysql-files/StepCount_20220117.csv'

That last line is not a valid MySql command. Assuming you want to write the query results to a file, you're missing the INTO OUTFILE clause:
MySQL :: MySQL 8.0 Reference Manual :: 13.2.10.1 SELECT ... INTO Statement[^]
SQL
SET @SQL :=Concat("select ...", " INTO OUTFILE ", CHAR(39), @outextension, CHAR(39));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900