1
-- 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');

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

这里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 演示

4

1 回答 1

1

将这样的值保留在表中很难且容易出错,您确定不想在某些视图中内联计算它吗:

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

sql fiddle demo

于 2013-08-28T06:28:41.813 回答