我的左外部联接在PostgreSQL中不起作用

问题描述 投票:0回答:2

我正在学习SQL,我需要进行查询以提供所有事件,但是我有特殊性。 FK id_festival可以为null,表事件中的列nb_tickets_sold也可以为null。

我试图进行以下查询,但没有结果

CREATE VIEW project.showEvents AS 
SELECT ev.id_event AS "ID",
ev.event_name AS "Name",
ev.price AS "Price",
fe.festival_name AS "Festival",
ev.event_date AS "Date",
ch.concert_hall_name AS "Hall"
FROM project.concerts co,  project.concert_halls ch,
project.events ev LEFT OUTER JOIN project.festivals fe ON ev.id_festival = fe.id_festival
WHERE ev.id_event = co.id_event
AND ev.id_concert_hall = ch.id_concert_hall
ORDER BY ev.event_date;
sql postgresql left-join
2个回答
0
投票

不要混合使用隐式和显式连接

  CREATE VIEW project.showEvents AS 
  SELECT ev.id_event AS "ID",
  ev.event_name AS "Name",
  ev.price AS "Price",
  fe.festival_name AS "Festival",
  ev.event_date AS "Date",
  ch.concert_hall_name AS "Hall"
  FROM project.concerts co
  INNER JOIN   project.concert_halls ch ON ev.id_concert_hall = ch.id_concert_hall
  INNER project.events ev  ON ev.id_event = co.id_event
  LEFT OUTER JOIN project.festivals fe ON ev.id_festival = fe.id_festival
  ORDER BY ev.event_date;

0
投票

我改变了,但是却一无所有

CREATE OR REPLACE VIEW project.showEvents AS 
SELECT ev.id_event AS "ID",
ev.event_name AS "Name",
ev.price AS "Price",
fe.festival_name AS "Festival",
ev.event_date AS "Date",
ch.concert_hall_name AS "Hall"
FROM project.concert_halls ch
INNER JOIN project.events ev ON ch.id_concert_hall = ev.id_concert_hall
INNER JOIN project.concerts co ON ev.id_event = co.id_event
LEFT OUTER JOIN project.festivals fe ON ev.id_festival = fe.id_festival
ORDER BY ev.event_date;
© www.soinside.com 2019 - 2024. All rights reserved.