2014-01-13 47 views
0

我需要在一個表中更新特定行,選擇該行我需要使用內部連接,我寫了這個:如何使用內部連接更新特定行?

UPDATE Items 
SET [Seq] = '0' 
WHERE EXISTS 
(SELECT * 
FROM Items 
inner join Dictionary on items.Member = Dictionary.Member WHERE 
Items.ID = '1' and Items.Member ='23') 

所有行項目表進行了更新,而不是特定的行(選擇語句工作正常,我得到我需要的行)

我想念什麼?

+0

你沒有在你的where子句指定一個字段您的子選擇應檢索的主字段只有你想要更新的ID號,所以它更像'WHERE ID IN(SELECT ID FROM Items ....)' – Dave

+0

你使用的是什麼RDBMS? – Mureinik

+0

@Dave - 我將其更改爲Items.ID(SELECT Items.ID ...),但仍然沒有改變。 – user2560521

回答

2

Sql Server的

UPDATE Items 
SET Items.[Seq] = '0' 
FROM Items inner join Dictionary 
on items.Member = Dictionary.Member 
WHERE Items.ID = '1' and Items.Member ='23' 

Mysql的

UPDATE Items 
INNER JOIN Dictionary 
ON items.Member = Dictionary.Member 
SET Items.[Seq] = '0' 
WHERE Items.ID = '1' and Items.Member ='23' 
+0

謝謝你,爲我工作。 – user2560521

+1

沒有問題很高興它幫助:) –

1

SQL Server中的格式是:

Update a 
set field1 = b.field2 
from table1 a 
join table2 b on a.id = b.table1id 
where b.somefield = 'test'