0

I am beginner in mysql Can anybody help me with the error on line number 5:

select a.*, count(b.user_id) as attempts from survey a 
left join survey_attempt b on a.survey_id = b.attempt_surveyid 
where a.survey_status != 0 
order by survey_order asc 
GROUP by a.survey_id
Dharman
  • 30,962
  • 25
  • 85
  • 135
Devanshu Saini
  • 765
  • 5
  • 24

3 Answers3

4

Your ORDER BY should be after GROUP BY.

Tibin Paul
  • 806
  • 9
  • 23
2

Use Group By clause before Order by Clause.

As below :

select a.*, count(b.user_id) as attempts from survey a 
left join survey_attempt b on a.survey_id = b.attempt_surveyid 
where a.survey_status != 0 
GROUP by a.survey_id
order by survey_order asc 
guri
  • 662
  • 2
  • 8
  • 26
1

You are not allowed to ORDER before you GROUP, so your query should look like this:

select a.*, count(b.user_id) as attempts 
from survey a 
   left join survey_attempt b on a.survey_id = b.attempt_surveyid 
where a.survey_status != 0 
group by a.survey_id
order by survey_order asc 

More details can be found here, where a similar question was asked and received more elaborated answers.

As a side note, try avoiding * and put required column instead.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164