首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有多个CTE的SQL查询,但得到“错误:关系"t”不存在“

带有多个CTE的SQL查询,但得到“错误:关系"t”不存在“
EN

Stack Overflow用户
提问于 2022-04-12 14:42:07
回答 1查看 96关注 0票数 -1

我试图使用多个CTE运行一个SQL查询,但得到"ERROR: relation“不存在

这是密码。我做错了什么?

代码语言:javascript
复制
WITH tmp AS (
        Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a 
        inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
        where a.tx_in_id = 0
    ),
    tmp2 AS
    (
    Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
    inner join txo as b ON 
        tmp.tx_in_id  = b.tx_id and 
        tmp.address = b.address
    inner join tx as c ON 
        tmp.tx_in_id = c.id
    )
    Update t 
    SET 
        t.tx_in_id = tmp2.tx_in_id, 
        t.value_next = tmp2.value_next, 
        t.consumed_epoch = tmp2.epoch_no,
        t.consumed_date = tmp2.date
    FROM txo as t
    inner join tmp2 ON
        t.tx_id = tmp2.tx_id and t.txo_index = tmp2.txo_index and t.address = tmp2.address
    where t.tx_in_id =0
EN

回答 1

Stack Overflow用户

发布于 2022-04-12 14:47:52

我认为在postgres中不允许将表作为UPDATESET子句中的别名。相反:

代码语言:javascript
复制
WITH tmp AS (
        Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a 
        inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
        where a.tx_in_id = 0
    ),
    tmp2 AS
    (
    Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
    inner join txo as b ON 
        tmp.tx_in_id  = b.tx_id and 
        tmp.address = b.address
    inner join tx as c ON 
        tmp.tx_in_id = c.id
    )
    Update txo
    SET 
        tx_in_id = tmp2.tx_in_id, 
        value_next = tmp2.value_next, 
        consumed_epoch = tmp2.epoch_no,
        consumed_date = tmp2.date
    FROM  tmp2 
    WHERE txo.tx_id = tmp2.tx_id 
        and txo.txo_index = tmp2.txo_index 
        and txo.address = tmp2.address
        and txo.tx_in_id =0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71844691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档