Warm tip: This article is reproduced from serverfault.com, please click

sql-更新同一张表中的特定列并添加限制

(sql - Update specific column within the same table and add limit)

发布于 2020-11-28 13:20:54

我想通过转换其原先的值来更新同一表中一列的值,但是我想根据其创建日期首先使用前10个数据对其进行测试。

我虽然收到此错误

ERROR:  syntax error at end of input
LINE 6: limit 10

这是我的示例代码:

UPDATE posts p1
SET cooked = (select entity2char(strip_tags(p2.cooked))
FROM posts p2
WHERE p1.id = p2.id
ORDER BY p2.created_at ASC 
LIMIT 10

有什么语法可以使这项工作正常吗?

Questioner
illumillukilluallukalluto
Viewed
0
Gordon Linoff 2020-11-28 21:39:16

嗯。我不明白你为什么使用自联接。如果只想删除前10个实体,则可以使用:

UPDATE posts p
    SET cooked = entity2char(strip_tags(p.cooked))
    WHERE p.id IN (SELECT p2.id
                   FROM posts p2
                   ORDER BY p2.created_at ASC 
                   LIMIT 10
                  );