2012-04-03 137 views
4

我試圖顯示有義務的志願者信息和分配的性能。 我想顯示這些信息。但是,當我運行查詢時,它沒有收集來自相同性能的不同日期。而且availability_date也被混淆了。它是否正確查詢它?我不確定這是正確的查詢。 你能爲我提供一些反饋嗎? 謝謝。使用多個表的Oracle查詢

enter image description here

查詢就在這裏。

SELECT Production.name, performance.performance_date, volunteer_duty.availability_date, customer.name "Customer volunteer", volunteer.volunteerid, membership.name "Member volunteer", membership.membershipid 
FROM Customer, Membership, Volunteer, volunteer_duty, duty, performance_duty, performance, production 
WHERE 
Customer.customerId (+) = Membership.customerId AND 
Membership.membershipId = Volunteer.membershipId AND 
volunteer.volunteerid = volunteer_duty.volunteerid AND 
duty.dutyid = volunteer_duty.dutyid AND 
volunteer_duty.dutyId = performance_duty.dutyId AND 
volunteer_duty.volunteerId = performance_duty.volunteerId AND 
performance_duty.performanceId = performance.performanceId AND 
Performance.productionId = production.productionId 

--Added image-- 結果: enter image description here

回答

5

查詢似乎是合理的,在具有什麼似乎是適當的加入所有表之間的條件,其條款。我不清楚你對結果有什麼問題;如果您更詳細地解釋和/或顯示數據的相關子集,可能會有所幫助。

但是,由於您說有一些與availability_date有關的問題,我首先想到的是您希望在該欄中具有某些條件,以確保志願者可以在給定的表演日期。這可能意味着只需將volunteer_duty.availability_date = performance.performance_date添加到查詢條件。

我的更一般的建議是從頭開始編寫查詢,每次添加一個表,並使用ANSI連接語法。這將更清楚哪些條件與哪些連接相關,並且如果您一次添加一個表格,希望您會看到結果出錯的地方。

舉例來說,我可能會與此開始:

SELECT production.name, performance.performance_date 
    FROM production 
     JOIN performance ON production.productionid = performance.productionid 

如果給出的結果有意義,那麼我會去上添加一個加盟performance_duty和運行查詢。等等。

+0

謝謝你的回覆。當我嘗試使用您的查詢時,它正在工作,但我如何使用ANSI連接語法來使用所有表?你能給我一些問題嗎?我添加了最後一個有問題的結果。請讓我知道。謝謝。 – wholee1 2012-04-03 21:09:43

3

我建議你明確寫入JOINS而不是使用WHERE語法。

使用內部連接你所描述的查詢,可能是這樣的:

SELECT * 
FROM volunteer v 
INNER JOIN volunteer_duty vd ON(v.volunteerId = vd.colunteerId) 
INNER JOIN performance_duty pd ON(vd.dutyId = pd.dutyId AND vd.volunteerId = pd.colunteerId) 
INNER JOIN performance p ON (pd.performanceId = p.performanceId) 
+1

+1,poeple應該使用SQL-92語法,而不是依附於過時的SQL-86語法。 – Ollie 2012-04-04 08:08:33