在mysql中使用限制更新多行?

2022-01-17 00:00:00 sql-update sql mysql
    UPDATE messages set test_read =1 
        WHERE userid='xyz' 
        ORDER BY date_added DESC  
        LIMIT 5, 5 ;

我正在尝试使用此查询来使用限制更新一组 5 行,但 mysql 显示错误..下面的一个正在工作

I am trying to use this query to update a set of 5 rows using limit but mysql is showing an error..The one below is working

    UPDATE messages set test_read =1 
        WHERE userid='xyz' 
        ORDER BY date_added DESC  
        LIMIT 5 ;

为什么第一个不起作用?

why is the first one not working?

推荐答案

如果你真的必须这样做,你可以使用这样的东西:

If you really must do it this way, you can use something like this:

 UPDATE messages SET test_read=1
 WHERE id IN (
     SELECT id FROM (
         SELECT id FROM messages 
         ORDER BY date_added DESC  
         LIMIT 5, 5
     ) tmp
 );

相关文章