我是PostgreSQL新手,我有两张这样的桌子:
Attendees(AttendeeId, Name)
Couples(CoupleId, AttendeeIdMan, AttendeeIdLady)
如何创建这样的视图?

Attendees_Couple(CoupleId, Name_Man, Name_Lady)

现在,我拿着下面的东西出来了,但后来我被卡住了。
CREATE VIEW Attendees_Couple AS
SELECT a."Name"
FROM "Attendees" a, "Couples" c
WHERE a."AttendeeID" = c."AttendeeIdMan"....

任何提示或帮助将不胜感激!

最佳答案

您必须加入Attendees两次:

CREATE VIEW "Attendees_Couple" AS
SELECT c."CoupleId",
       a1."Name" AS "Name_Man",
       a2."Name" AS "Name_Lady"
FROM "Couples" AS c
   JOIN "Attendees" AS a1 ON c."AttendeeIdMan"  = a1."AttendeeId"
   JOIN "Attendees" AS a2 ON c."AttendeeIdLady" = a2."AttendeeId";

帮你自己个忙,用小写的名字!

关于sql - 如何通过联接PostgreSQL中的两个表来创建 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53528173/

10-16 22:42