开发者

update a column of the first 5000 rows of a table in MYSQL version 5.1.11

I have a situation in which I need to update some values of a column in a table in batches(i.e. first 50开发者_运维技巧00 rows).

So I wrote a query as

update clientdetails set lastupdateddate=now where id in (Select id from clientdetails limit 1,5000);

The error is prompted as this version of MYSQL doesn't support Limit 1,5000. So is there any other way of doing this?


Couldn't you just do it this way:

update clientdetails set lastupdateddate=now where <your where clause> 
limit <desired limit>;

?


MySQL has a couple of problems with UPDATE and sub-queries.

SELECT * FROM table1 WHERE foo IN (SELECT foo FROM table2 LIMIT 50)

is not possible. But the following is:

SELECT * 
FROM table1 t
JOIN (SELECT foo FROM table2 LIMIT 50) as tmp
ON tmp.foo = t.foo;

I beliebe this "(query) as table" thing is called a "virtual temporary view" (but I may be wrong here...)

The second Problem you're facing is that you can't udate a table you're currently reading from.

But (as Griwes pointed out) the UPDATE knows a LIMIT:

UPDATE clientdetails 
SET lastupdateddate = NOW() 
ORDER BY id
LIMIT 0, 5000;

should work. Make sure you've got the ORDER BY in there, otheriwse you might be UPDATING some rows twice and some never.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜