2016-11-08 46 views
3

我有3個表格:te_event,te_venue,te_category。如何加入3表而不重複ID?

te_event表中的列:categoryID, venueID, event description, title, date, price.

te_venue表中的列:venueID, venueName and location.

te_category有列:catID and catDesc.

這裏是我的查詢

SELECT * 
FROM te_events 
INNER JOIN te_venue 
     ON te_events.venueid = te_venue.venueid 
INNER JOIN te_category 
     ON te_events.catid = te_category.catid 

,但它表明了這樣的通過重複ID

eventID | eventTitle | eventDescription |venueID | catID| eventStartDate |eventEndDate |eventPrice | venueID | venueName | location| catID | catDesc

+0

加入可以在一個表中引起的一組值重複,如果創紀錄的比賽超過一次。使用'DISTINCT'_might_給你你想要的。 –

+0

[如何在多行中使用相同ID在MYSQL中連接2個表]可能的重複(http://stackoverflow.com/questions/18103551/how-to-join-2-tables-in-mysql-with-same- id-on-multiple-rows) – SnakeEye

+0

提供來自每個表的示例數據,獲取的數據和您的預期結果 – Viki888

回答

1
SELECT e.event_description, e.title,e.date,e.price,v.venueName,v.location,c.catDesc 
    FROM te_events e 
    INNER JOIN te_venue v ON e.venueID = v.venueID 
    INNER JOIN te_category c ON e.catID = c.catID 

見下

enter image description here

+0

它應該像SELECT te_events.eventID,te_venue.venueName,te_category.catDesc FROM te_events INNER JOIN te_venue ON te_events .venueID = te_venue.venueName INNER JOIN te_category ON te_events.catID = te_category.catID; 但它不會返回任何東西 –

+0

是你得到任何異常或沒有得到任何結果? –

+0

什麼都得不到.... –

1

我的結果爲什麼不選擇只列您需要什麼?例如:

 SELECT E.eventID, E.eventTitle, E.eventDescription, 
      E.eventStartDate, E.eventEndDate, E.eventPrice 
      E.catID, V.venueID, V.venueName, V.location 
      FROM te_events AS E 
      INNER JOIN te_venue AS V ON E.venueID = V.venueID 
      INNER JOIN te_category AS C ON E.catID = C.catID 

它可能是一個有點長,但它仍然可能最有可能給你你所期望的結果。

乾杯&祝你好運。

+0

更長什麼? – alirakiyan

+0

@alirakiyan **鍵入上述所有的字段名稱可能會比輸入'SELECT *'長一些 - **這就是該語句中的含義**; - )** – Poiz

0
SELECT DISTINCT t1.categoryID,t1.venueID,t1.eventDescription,t1.title,t1.date,t1.price,t2.venueName, t2.location, t3.catDesc FROM te_events AS t1 
       INNER JOIN te_venue AS t2 ON te_events.venueID = te_venue.venueID 
       INNER JOIN te_category AS t3 ON te_events.catID = te_category.catID 

我希望它能幫助