mysql5.7的3065錯誤解決方案

有以下sql

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%';

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章