使用CTE我尝试
WITH cte as (
SELECT myFieldName,
row_number() over (order by gps_device_id) as rn
FROM tracker.gps_devices
)
UPDATE cte
SET cte.myFieldName = CASE WHEN cte.rn % 3 = 0 THEN '0'
WHEN cte.rn % 3 = 1 THEN '1'
WHEN cte.rn % 3 = 2 THEN '2'
END
但是出现了以下错误。
错误:关系“cte”不存在
看起来INSERT
DELETE
WITH
UPDATE
但只有{c}内的UPDATE tracker.gps_devices g
SET g.myFieldName = CASE WHEN t.rn % 3 = 0 THEN '0'
WHEN t.rn % 3 = 1 THEN '1'
WHEN t.rn % 3 = 2 THEN '2'
END
FROM (SELECT gps_device_id,
myFieldName,
row_number() over (order by gps_device_id) as rn
FROM tracker.gps_devices) as t
WHERE g.gps_device_id = t.gps_device_id
,这是正确的吗?我确定我做过这样的事情,但也许是在不同的数据库中。
https://www.postgresql.org/docs/9.6/static/queries-with.html
所以我以此结束,即使工作很混乱,有什么建议吗?
router.get('/service/products/all', function(req, res, next) {
Product.find({ 'PMC18': {$ne: null} }, { prodCode: 1, prodName: 1, description: 1, PMC18: 1, inStock: 1 }, function (err, docs) {
console.log(err);
console.log(docs);
res.json(docs);
}).limit(50);
});
答案 0 :(得分:4)
您可以使用cte进行更新,例如(假设id是主键):
with cte as (
select
id,
my_field_name,
row_number() over (order by gps_device_id) as rn
from gps_devices
)
update gps_devices
set my_field_name = (rn % 3)::text
from cte
where gps_devices.id = cte.id;
您可以插入,更新或删除表(视图)的行,而不是查询的结果集。