Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 恢复没有现有主键的审核表的最佳方法-postgres_Sql_Postgresql_Sql Update_Common Table Expression - Fatal编程技术网

Sql 恢复没有现有主键的审核表的最佳方法-postgres

Sql 恢复没有现有主键的审核表的最佳方法-postgres,sql,postgresql,sql-update,common-table-expression,Sql,Postgresql,Sql Update,Common Table Expression,表A有一个审计表,我需要从中还原特定列 从表A中删除了行,然后我重新生成了行,通过在特定时间匹配这些是约会记录,我使用以下SQL找到了表A与其审计表之间的匹配 select b_aud.meta, a.id as a_id, b.id as b_id from a join b on a.id = b.id join a_aud on a.course_id = a_aud.course_id and a.occurrence_start_date_t

表A有一个审计表,我需要从中还原特定列

从表A中删除了行,然后我重新生成了行,通过在特定时间匹配这些是约会记录,我使用以下SQL找到了表A与其审计表之间的匹配

select 
    b_aud.meta,
    a.id as a_id,
    b.id as b_id
from a
join b on a.id = b.id
join a_aud on 
    a.course_id = a_aud.course_id and
    a.occurrence_start_date_time = a_aud.occurrence_start_date_time and
    a.occurrence_end_date_time = saa_audoccurrence_end_date_time and
    a.tutor_id = a_aud.tutor_id and
    a.student_ids = a_aud.student_ids and
    a.term_id = a_aud.term_id
join b_aud on 
    b_aud.student_id = b.student_id and
    b_aud.session_id = a.ac2_session_id
where 
    a_aud.audit_action = 'DELETE' and
    a.occurrence_start_date_time <= current_timestamp and
    b_aud.meta::text != '{}'
更新是为了更新表B上的“meta”列 模式:

表B中的1行表示表A中学生ID中的每个值

例如

a:
1 (pk)
'2020-01-01 00:00:00'
'2020-01-01 01:00:00'
[1,2]
1
1

b:
1 (pk) 
1 (fk to a)
1(student_id)
{'note': 'something'}

b:
2 (pk) 
1 (fk to a)
2 (student_id)
{'note': 'something'}

根据您在问题中提出的查询,如果我理解正确,您希望将第一次查询生成的数据更新到表B中。假设您的第一次查询工作正常,请尝试以下查询:

with report_answers as (
select 
    b_aud.meta,
    a.id as a_id,
    b.id as b_id
from a
join b on a.id = b.id
join a_aud on 
    a.course_id = a_aud.course_id and
    a.occurrence_start_date_time = a_aud.occurrence_start_date_time and
    a.occurrence_end_date_time = saa_audoccurrence_end_date_time and
    a.tutor_id = a_aud.tutor_id and
    a.student_ids = a_aud.student_ids and
    a.term_id = a_aud.term_id
join b_aud on 
    b_aud.student_id = b.student_id and
    b_aud.session_id = a.ac2_session_id
where 
    a_aud.audit_action = 'DELETE' and
    a.occurrence_start_date_time <= current_timestamp and
    b_aud.meta::text != '{}'
)

update b t1
set meta= t2.meta
from report_answers t2 join a t3 on t3.id = t2.a_id
where t1.id=t2.b_id

注意:我认为在更新查询中不需要表a的联接。您可以根据需要使用或删除它。

您要更新A或B的哪个表以及您要更新的数据?表B,我可以将模式添加到问题中。请使用一些示例添加模式data@AkhileshMishra我已经添加了schema和example,我已经根据您的查询添加了答案
a:
1 (pk)
'2020-01-01 00:00:00'
'2020-01-01 01:00:00'
[1,2]
1
1

b:
1 (pk) 
1 (fk to a)
1(student_id)
{'note': 'something'}

b:
2 (pk) 
1 (fk to a)
2 (student_id)
{'note': 'something'}
with report_answers as (
select 
    b_aud.meta,
    a.id as a_id,
    b.id as b_id
from a
join b on a.id = b.id
join a_aud on 
    a.course_id = a_aud.course_id and
    a.occurrence_start_date_time = a_aud.occurrence_start_date_time and
    a.occurrence_end_date_time = saa_audoccurrence_end_date_time and
    a.tutor_id = a_aud.tutor_id and
    a.student_ids = a_aud.student_ids and
    a.term_id = a_aud.term_id
join b_aud on 
    b_aud.student_id = b.student_id and
    b_aud.session_id = a.ac2_session_id
where 
    a_aud.audit_action = 'DELETE' and
    a.occurrence_start_date_time <= current_timestamp and
    b_aud.meta::text != '{}'
)

update b t1
set meta= t2.meta
from report_answers t2 join a t3 on t3.id = t2.a_id
where t1.id=t2.b_id