第一次出现其他field = value时更新字段?

时间:2013-09-11 03:26:59

标签: mysql

Here's the following SQL in SQL Fiddle.

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

如果不知道该记录的ID,如何将SET content = "test 1"仅应用于第一条记录 WHERE rel = 2 ORDER BY id ASC?是否可以在没有任何子查询的情况下执行此操作?

1 个答案:

答案 0 :(得分:3)

假设'first'表示结果按id排序的第一行:

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