嗨是否可以使用限制和偏移量的其他表更新表格值?
例如表t_device有600行 和t_object有100行
我想更新一列,但只更新前100行,其中包含来自子查询的偏移量,如
update t_device set id_object =
(select id_object from t_object limit 100) limit 100 offset 0;
update t_device set id_object =
(select id_object from t_object limit 100) limit 100 offset 100;
有可能吗?我无法找到一个解决方案来修改t_object
中t_device中的值即时通讯使用postgres 8.4
答案 0 :(得分:8)
以下内容可能适合您。
UPDATE t_device set id_object = t.id_object
FROM (select id_object from t_object order by id_object limit 100) t
WHERE t.id_object = t_device.id_object;
答案 1 :(得分:1)
我通过生成列rowNumber来解决它,并使用增量值(1,2,3)更新它
然后
UPDATE t_device set id_object = t.id_object FROM
(select t1.id_object , row_number() OVER (ORDER BY id_object ) AS rn
from dblink('dbname=*** password=***', 'select id_object from t_object')
as t1(id_object int) order by id_object limit 103) as t
where t_device.rowNumber = t.rn