MySQL中的UPDATE和LIMIT [英] `UPDATE` and `LIMIT` in `MySQL`

查看:948
本文介绍了MySQL中的UPDATE和LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新特定范围的行,例如从30开始并在50结束.我该如何实现呢?

I would like to update a specific range of rows, say starting from 30 and ending at 50. How may I achieve that.

我尝试过:

UPDATE tab
SET    col = 'somevalue' 
LIMIT 30, 50

但这是行不通的.有什么办法可以更新这些行?

but this doesn't work. Is there any way that I can update these rows?

我得到的错误是:

检查手册...以获取在'50'附近使用的正确语法

Check the manual ... for the right syntax to use near ' 50'

推荐答案

您的语句不是有效的MySQL语法 ,而且这没有任何意义.语法的问题是update语句不支持偏移量(请参见这里).

Your statement is not valid MySQL syntax and it doesn't make sense. The problem with the syntax is that offset is not supported for update statements (see here).

逻辑问题是您没有order by子句. MySQL在处理表时不保证表的顺序.因此,第一"二十行和第二"二十行没有区别.

The problem with the logic is that you have no order by clause. MySQL doesn't guarantee the order of tables when processing them. So the "first" twenty rows and the "next" twenty" rows make no difference.

这为什么不做你想要的?

Why doesn't this do what you want?

UPDATE tab
  SET    col = 'somevalue' 
  LIMIT 20;

如果您有指定顺序的特定列,则可以使用where:

If you have a specific column that specifies the ordering, you can use where:

UPDATE tab
  SET    col = 'somevalue' 
  wHERE ID >= 30 and ID < 50;

这篇关于MySQL中的UPDATE和LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆