更新语句有限制

时间:2015-07-21 18:30:05

标签: sql

update accounts a
left join users b on
    a.user_id = b.user_id
set `sold` = '1' where b.country = 'UA' AND a.site = 'od'

如何限制此查询?

尝试

update accounts a
left join users b on
    a.user_id = b.user_id
set `sold` = '1' where b.country = 'UA' AND a.site = 'od' LIMIT 500

但是获取错误错误:UPDATE和LIMIT的使用不正确

2 个答案:

答案 0 :(得分:0)

试试这个

update accounts a left join users b on a.user_id = b.user_id
set 'sold' = '1'
where b.country in
(select country from
(select country from users where
country = 'UA' order by user_id asc limit 500) tmp) and 
a.site in
(select site from
(select site from accounts where
site = 'od' order by user_id asc limit 500) tmp2);

有关选择限制声明的更多详细信息,请参阅此处。

http://www.techonthenet.com/sql/select_limit.php

答案 1 :(得分:0)

我将首先使用CTE表(临时表)来保存限制查询,然后使用该表执行更新:

With LimitQuery As
(
select * from  accounts a
left join users b on
    a.user_id = b.user_id
where b.country = 'UA' AND a.site = 'od' LIMIT 500
)

update LimitQuery 
set `sold` = '1'