postgresql Sql联接2个表,userId在外部

qrjkbowd  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(119)
CREATE TABLE event (
   id SERIAL PRIMARY KEY,
   city VARCHAR ( 30 ),
   author_id INT REFERENCES users (id) NOT NULL,
   text VARCHAR,
   cDate TIMESTAMPTZ NOT NULL DEFAULT NOW(),
   uDate TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TYPE InviteStatus AS ENUM ('pending', 'approved', 'declined');
CREATE TABLE invite  (
   sender_id INT REFERENCES users (id) NOT NULL,
   receiver_id INT REFERENCES users (id) NOT NULL,
   event_id INT REFERENCES event (id) NOT NULL,
   receiver_approved InviteStatus DEFAULT 'pending' NOT NULL,
   cDate TIMESTAMPTZ NOT NULL DEFAULT NOW(),
   uDate TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

我们有两个表。我们获取userId,即sender_id。如果userId = sender_id作为来自外部的参数存在于invite表中,并且www.example.com = invite.event_id,我们需要从event获取所有列,并在结果中从invite创建字段receiver_approvedevent.id

irlmq6kh

irlmq6kh1#

SELECT e.*, i.receiver_approved
FROM invite i
JOIN event e on e.id = i.event_id
WHERE i.sender_id = :userID

其中userID是来自外部的参数

相关问题