Oracle SQL开发人员如何显示外键的NULL值

时间:2012-03-28 11:36:47

标签: oracle

我将尝试在Ticket和Membership之间使用左外连接。 但是,它不会在Ticket上显示NULL值的外键。你能给我一些答案吗?这个查询有什么问题吗? 感谢。

FROM Ticket t, Production pro, Performance per, Price, Price_level, Booking, Customer, Customer_Concession ccons, Membership, Member_concession mcons
WHERE t.performanceid = per.performanceid AND
  t.PRODUCTIONID = Price.PRODUCTIONID AND
  t.levelId = Price.levelId AND
  Price.PRODUCTIONID = pro.PRODUCTIONID AND
  Price.levelId = Price_level.levelId AND
  Booking.bookingId (+) = t.bookingId AND
  Customer.customerId = Booking.customerId AND
  ccons.cConcessionId (+) = Customer.cConcessionId AND  
  Membership.membershipId (+) = t.membershipId AND
  Membership.mConcessionId = mcons.mConcessionId 
ORDER BY t.ticketId

2 个答案:

答案 0 :(得分:4)

您遇到的一个潜在问题是这两个条件:

  Booking.bookingId (+) = t.bookingId AND
  Customer.customerId = Booking.customerId AND

由于您正在对Booking进行外部联接,因此当找不到匹配项时,其列将显示为NULL;但随后你正常加入Customer,所以这些行将被删除,因为NULL不能等于任何东西。您可能还想将第二行更改为外连接。

但是,我不知道这是否是你的主要问题,因为我实际上并不确切地理解你在问什么。 “外键的NULL值”是什么意思?您尚未指定外键是什么。

答案 1 :(得分:1)

为了扩展Dave的观察,并给你一个SQL92语法的例子,请学习它并远离Oracle自己的外连接语法。

FROM
TICKET t
JOIN Performance per
ON per.performance_id = t.performance_id
JOIN Production pro
ON pro.produciton_id = t.production_id
JOIN PRICE pr
ON pr.production_id = pro.production_id
AND pr.levelId = t.level_id
JOIN price_level pl
ON   pl.levelid = pr.levelid
LEFT OUTER JOIN booking b
on  b.booking_id = t.booking_id
LEFT OUTER JOIN customer cus
on  cus.customer_id = b.customer_id
LEFT OUTER JOIN customer_concession cons
ON cons.concession_id = cus.concession_id
LEFT OUTER JOIN memebership m
ON M.membership_id = t.membership_id
LEFT OUTER JOIN membership_concession mc
ON mc.mConcession_id = m.mConcession_id
Order by t.ticketid