我有以下INSERT语句:
33 INSERT INTO
34 Exhibitions (
35 ex_eName,
36 ex_eDescription,
37 ex_eDateStart,
38 ex_eDateEnd)
39 VALUES
40 ('exhibition name',
41 'exhibition description',
42 (SELECT
43 dateStart
44 FROM
...
62 ),
63 '2017-10-10'); -- this date is a placeholder
64
我在4个领域的展览中插入记录:展览名称,展览描述,开始日期,结束日期。我正在使用一些子查询来查找开始日期(我取出了子查询,因为它与问题无关)。
结束日期应为开始日期后5个月。在我定义结束日期字段时,我无法弄清楚如何引用开始日期字段。这是我想要做的一个例子:
33 INSERT INTO
34 Exhibitions (
35 ex_eName,
36 ex_eDescription,
37 ex_eDateStart,
38 ex_eDateEnd)
39 VALUES
40 ('exhibition name',
41 'exhibition description',
42 (SELECT
43 dateStart
44 FROM
...
62 ),
63 dateStart + interval '5 months');
64
这在PostgreSQL中是否可行?
答案 0 :(得分:1)
使用insert . . . select
:
INSERT INTO Exhibitions (ex_eName, ex_eDescription, ex_eDateStart, ex_eDateEnd)
SELECT 'exhibition name', 'exhibition description', datestart,
dateStart + interval '5 months'
FROM . . .;