【问题标题】:Adding INTERVAL to TIME in SQL在 SQL 中将 INTERVAL 添加到 TIME
【发布时间】:2019-01-07 06:24:43
【问题描述】:

在 PostgreSQL 中(使用 pgAdmin 3...这是我的学校要求我们使用的)当我尝试签入触发器时,如果 NEW 事件与另一个事件同时发生,我会收到一条错误消息在。 'stime' 属于 TIME 数据类型,代表“开始时间”,而 'duration' 属于数据类型 INTERVAL。

if (NEW.stime >= schedule.stime AND NEW.stime <= (schedule.stime + schedule.duration))
--Checking if the new event happens while another event is.

给出的错误如下:

    ERROR:  missing FROM-clause entry for table "schedule"
LINE 5:   (NEW.stime >= schedule.stime AND NEW.stime <= (schedule.st...
                        ^
QUERY:  SELECT (NEW.sdate IN (SELECT sdate FROM schedule)) AND
        --Checking for events in the room at given date.
        (NEW.rno IN (SELECT rno FROM schedule WHERE NEW.sdate = schedule.sdate AND NEW.bno = schedule.bno)) AND
        --Checking if the new event happens while another event is.
        (NEW.stime >= schedule.stime AND NEW.stime <= (schedule.stime + schedule.duration))
CONTEXT:  PL/pgSQL function trigf1() line 12 at IF
********** Error **********

ERROR: missing FROM-clause entry for table "schedule"
SQL state: 42P01
Context: PL/pgSQL function trigf1() line 12 at IF

我还在此处发布了房间和日程安排所需的 CREATE 以及触发器:

CREATE TABLE room (

rno INT     NOT NULL,

bno INT     NOT NULL,

function VARCHAR (30),

capacity INT,

PRIMARY KEY (rno, bno)

);

CREATE TABLE schedule (

sdate DATE  NOT NULL,

stime TIME  NOT NULL,

rno INT     NOT NULL,

bno INT     NOT NULL,

eid INT     NOT NULL,

duration INTERVAL,

FOREIGN KEY (rno, bno) REFERENCES room (rno, bno)

CREATE OR REPLACE FUNCTION trigf1() RETURNS TRIGGER AS $$

DECLARE

BEGIN
    IF 
    --Checking if the new event is in revent or sevent
    (NEW.eid IN (SELECT reid FROM revent) OR (NEW.eid IN (SELECT seid FROM sevent))) AND
    --Checking if the room exsists
    (NEW.rno IN (SELECT rno FROM room)) AND 
    (NEW.bno IN (SELECT bno FROM room WHERE NEW.rno = room.rno) ) THEN
        IF
        --Checking for other events on the same day.
        (NEW.sdate IN (SELECT sdate FROM schedule)) AND
        --Checking for events in the room at given date.
        (NEW.rno IN (SELECT rno FROM schedule WHERE NEW.sdate = schedule.sdate AND NEW.bno = schedule.bno)) AND
        --Checking if the new event happens while another event is.
        (NEW.stime >= schedule.stime AND NEW.stime <= (schedule.stime + schedule.duration)) THEN
        BEGIN
            RAISE NOTICE 'ERROR: Could not add given row to schedule';
            RETURN NULL;
        END;
        END IF;
    END IF;
    RETURN NEW;

END;
$$ LANGUAGE plpgsql;


CREATE TRIGGER T1 BEFORE INSERT ON schedule

FOR EACH ROW

EXECUTE PROCEDURE trigf1();

【问题讨论】:

  • 这留下了很多空白。了解错误消息可能会对初学者有所帮助。
  • 对!我将编辑帖子以包含错误

标签: sql postgresql sqldatatypes


【解决方案1】:

如果没有查询,您将无法访问来自 schedule 的行。尝试改变

(NEW.stime >= schedule.stime AND NEW.stime <= (schedule.stime + schedule.duration))

到:

EXISTS (SELECT *
               FROM schedule
               WHERE new.stime >= schedule.stime
                     AND new.stime <= (schedule.stime + schedule.duration))

但是你的构造似乎检查了每一个想要的条件,只有当一切正常时,它才会抛出一个错误。也许你需要否定一切。

【讨论】:

  • 好的,我会在几分钟后发布更广泛的代码
  • @TalMushkin:这或多或少是我的想象。所以我的解决方案应该普遍适用。但也许您的IFs 正在做与您想要的相反的事情?查看我的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多