2013-10-28 86 views
1

我需要使用連接和限制來更新表。我構建此查詢Mysql更新通過加入並限制

UPDATE table1 
JOIN table2 AS b 
ON table1.id = b.id 
SET 
table1.username = b.post_username 
WHERE b.post_username != '' 
AND table1.username = '' 
LIMIT 10 

unfortunaetly我recive錯誤:

Error Code: 1221 
Incorrect usage of UPDATE and LIMIT 

我該如何解決這個問題?

+0

你不能用限制這樣的更新 –

回答

0

恐怕你不能做到這一點:

如果你讀了documentation它說:

With no WHERE clause, all rows are updated. If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated.

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

,所以你不能在你的查詢中做到這一點。

0

哎只是刪除LIMIT 10從您的代碼

+0

很抱歉,但這些修改完成任何查詢類似的結果。而且它們只能用於更新單個表格。 –

0

您可以使用下面的查詢語法:

update work_to_do as target 
    inner join (
     select w. client, work_unit 
     from work_to_do as w 
     inner join eligible_client as e on e.client = w.client 
     where processor = 0 
     order by priority desc 
     limit 10 
    ) as source on source.client = target.client 
     and source.work_unit = target.work_unit 
    set processor = @process_id;