2013-09-11 42 views
2

Here's the following SQL in SQL Fiddle.第一次發生other_field = value時更新字段?

+-----+-----+-------------+ 
| id | rel | content  | 
+-----+-----+-------------+ 
| 1 | 1 | example 1 | 
| 2 | 2 | test 2 oops | 
| 3 | 1 | example 2 | 
| 4 | 2 | test 2  | 
+-----+-----+-------------+ 

如何申請SET content = "test 1"僅在第一記錄WHERE rel = 2 ORDER BY id ASC不知道該記錄的ID?沒有任何子查詢可以這樣做嗎?

+0

有你嘗試與限制1更新? –

+0

http://stackoverflow.com/questions/11424861/mysql-update-first-instance-of-a-record –

+0

@DipeshParmar'JOIN'是一個子查詢,不是嗎? –

回答

3

假設「第一」意味着第一行的結果時,由id下令:

update example set content = 'test 1' where rel = 2 order by id limit 1; 
+1

對不起,我認爲每個人都會認爲按id升序排序。我在我的問題上加了一個說明。另外,我不認爲'UPDATE'和'LIMIT'可以一起使用。感謝你的回答。 –

+1

錯誤代碼:1221 UPDATE和ORDER BY的用法不正確 –