gpt4 book ai didi

postgresql - 自动更新

转载 作者:行者123 更新时间:2023-11-29 13:33:32 30 4
gpt4 key购买 nike

-- Rough Work for MMID Updation Automatically

CREATE TABLE main
(
sno serial NOT NULL,
mid integer,
mmid integer,
venue text,
CONSTRAINT pk_sno PRIMARY KEY (sno)
);


INSERT INTO main(mid, mmid, venue) VALUES(1, 1, 'Ramesh Gardens');
INSERT INTO main(mid, mmid, venue) VALUES(1, 2, 'Suresh Gardens');
INSERT INTO main(mid, mmid, venue) VALUES(1, 3, 'Kamesh Gardens');
INSERT INTO main(mid, mmid, venue) VALUES(2, 1, 'Rajesh Gardens');
INSERT INTO main(mid, mmid, venue) VALUES(2, 2, 'Rajesh Gardens');

每当根据 mid 值的变化插入新记录时,我需要将列 mmid 值更新为 1

这里的mid代表Meeting_IDmmid代表SubMeeting_ID。我试过这样。

SELECT * FROM main;

INSERT INTO main(mid, mmid, venue) VALUES((SELECT MAX(mid) FROM main)+1,(SELECT MAX(mmid) FROM main WHERE mid=(SELECT MAX(mid) FROM main))+1,'Rajesh Gardens');

SELECT * FROM main;

SQL Fiddle demo

最佳答案

在表中保留这样的值很难且容易出错,您确定不想在某些 View 中直接计算它吗:

select
sno, mid, venue, row_number() over(partition by mid order by sno) as mmid
from main

sql fiddle demo

关于postgresql - 自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480698/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com