Incorrect usage of UPDATE and ORDER BY

今天想弄一个分类复习笔记,根据笔记类型来分配每天复习的比例,每种类型又按复习时间来排序,所以写下这个sql

UPDATE chicai_note INNER JOIN chicai_note_book ON chicai_note.relate_id = chicai_note_book.id INNER JOIN chicai_note_folder ON chicai_note_book.relate_id = chicai_note_folder.id 
SET chicai_note.isn_review = 0 WHERE chicai_note_folder.id = 6 ORDER BY chicai_note.review_at LIMIT 10

结果错误了,Incorrect usage of UPDATE and ORDER BY,看来这个Order By是有限制的

解决办法就是先select处理标记的id再和需要update的表联合,这就是只有一个联合,不存在Order By了

UPDATE chicai_note note INNER JOIN 
(select n.id from chicai_note n INNER JOIN chicai_note_book ON n.relate_id = chicai_note_book.id INNER JOIN chicai_note_folder ON chicai_note_book.relate_id = chicai_note_folder.id WHERE n.user_id=1 AND chicai_note_folder.id = 1 ORDER BY n.review_at LIMIT 10) 
AS temp ON note.id=temp.id SET note.isn_review = 1;

这是我写过最长的sql了,哈


首页 我的博客
粤ICP备17103704号