2012-02-19 104 views
1

我在創建SQL查詢時遇到了一些問題。3個表的MySQL查詢

我有以下表格:

**Tour** 
tourID 
tourName 

**Schedule** 
scheduleID 
tourID 

**Ticket** 
scheduleID 
customerID 

而我要選擇的是tourname和每班售出門票數量。我知道如何選擇按時間表銷售的門票數量,但我堅持讓它與巡迴賽鏈接。

號按計劃門票:

SELECT t.scheduleID, COUNT(t.scheduleID) AS numTickets 
FROM Ticket t 
GROUP BY t.scheduleID 

欣賞的幫助

回答

2

試試這個:

Select t.tourname, COUNT(tc.scheduleID) AS numTickets 
From Tour t 
inner join Schedule s on t.tourID = s.tourID 
inner join Ticket tc on s.scheduleID = tc.scheduleID 
group by t.tourID, t.tourname 
+0

這是完美的!非常感謝 :) – AzaraT 2012-02-19 14:34:20