Error in query: SELECT races.shortRaceName as racename, DATE_FORMAT(date,"%e%b%y" ) , place, results.firstname, results.lastname, results.city, results.guntime, results.categoryplace , results.gender, results.category, races.month, races.year , races.raceCode , races.distance_new As distance, resultid , '', results.chiptime , results.resultgroup, if(results.ratingpoints>0,ratingpoints,'') as points , results.ratingatdistance, races.distancemeters, if( length(results.chiptime) > 0 , results.chiptime , results.guntime ) as racetime, results.ratingseconds , results.locked, users_new.firstname, users_new.lastname, users_new.city , results.ratingdirty, (select a.guntime from results a where a.racecode = races.racecode and a.resultgroup = LIMIT 1) AS sameRace, races.date as reportdate, 'RRR' as source, if( results.chiptime_sec > 0 , results.chiptime_sec , results.guntime_sec ) AS racepersonal, races.MaleCount AS maleCount, races.FemaleCount AS femaleCount, results.genderplace As genderplace FROM results INNER JOIN races ON (results.racecode = races.raceCode) left join users_new on results.resultgroup = users_new.resultgroup WHERE results.resultgroup = '371738' UNION SELECT resultsuser.racename, DATE_FORMAT(racedate,"%e%b%y" ) as date, resultsuser.place, users_new.firstname, users_new.lastname, users_new.city , resultsuser.guntime, resultsuser.categoryplace , '', resultsuser.category, DATE_FORMAT(racedate, "%b") as month, DATE_FORMAT(racedate, "%y") as year , resultsuser.id , resultsuser.distance As distance, resultsuser.id , '', chiptime , resultsuser.resultgroup, '' as points , '', resultsuser.distancemeters, if( length(resultsuser.chiptime) > 0 , resultsuser.chiptime , resultsuser.guntime ) as racetime, 0 , '' , users_new.firstname, users_new.lastname, users_new.city , '', '' AS sameRace, resultsuser.racedate as reportdate, 'User' as source, if( resultsuser.chiptime_sec > 0 , resultsuser.chiptime_sec , resultsuser.guntime_sec ) AS racepersonal, 0 AS maleCount, 0 AS femaleCount, 0 AS genderplace FROM resultsuser left join users_new on resultsuser.resultgroup = users_new.resultgroup where resultsuser.resultgroup = '371738' order by reportdate DESC, racename , 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 'LIMIT 1) AS sameRace, races.date as reportdate, ' at line 30