Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql ORDER BY和GROUP BY的正确顺序是什么_Mysql - Fatal编程技术网

Mysql ORDER BY和GROUP BY的正确顺序是什么

Mysql ORDER BY和GROUP BY的正确顺序是什么,mysql,Mysql,我是mysql的初学者 有人能帮我解决第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 在orderby子句之前使用groupby子句 详

我是mysql的初学者 有人能帮我解决第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

orderby
子句之前使用
groupby
子句

详情如下:

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 

您的ORDER BY应该在GROUP BY之后。

您不允许在您的GROUP BY之前下单,因此您的查询应该如下所示:

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 
可以找到更多细节,其中提出了一个类似的问题,并得到了更详细的回答


作为旁注,请尽量避免使用
*
,并改为使用必填列

切换“分组依据”和“排序依据”在这一行中指定表名
order by a.survey\u order asc
。您想做什么?我试图创建“分组依据”和“排序依据”,我从下面的帮助中得到了答案,谢谢大家