SELECT DISTINCT questionid,questiontype
FROM x2_questions AS questions,x2_quest2knows AS quest2knows
WHERE find_in_set(quest2knows.qkknowsid,:knowsid)
AND quest2knows.qktype = 0
AND quest2knows.qkquestionid = questions.questionid
AND questions.questionstatus = 1
ORDER BY questionparent asc,questionsequence
运行时提示错误信息
Expression #1 of ORDER BY clause is not in SELECT list, references column ‘pe6.questions.questionparent‘ which is not in SELECT list; this is incompatible with DISTINCT
错误原因:
mysql5.7版本中,如果DISTINCT和order by一起使用将会报3065错误,sql语句无法执行。这是由于5.7版本语法比之前版本语法要求更加严格导致的。
解决方案:
修改mysql的安全模式,即sql_mode。
步骤:
1.mysql终端中输入下面命令,查看相关配置信息
show variables like ‘%sql_mode%‘;
默认情况下可以看到如下信息
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
2.设置sql_mode去掉ONLY_FULL_GROUP_BY
推荐在mysql的配置文件my.cnf文件(linux)/my.ini文件(window) 的mysqld中增加或者修改sql_model配置选项
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
3.重启mysql生效
4.验证
在mysql终端中输入下面命令,查看结果。
show variables like ‘%sql_mode%‘;
原文:https://blog.51cto.com/phpme/2443158