如何在不重复ID的情况下连接3个表格?

3

我有三个表:te_event、te_venue和te_category。

te_event表包含以下列:categoryID、venueID、event description、title、date和price。

te_venue表包含以下列:venueID、venueName和location。

te_category表包含以下列:catID和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


如果一个记录匹配超过一次,连接操作可能会导致一个表中的一组值重复。使用 DISTINCT 可能会得到你想要的结果。 - Tim Biegeleisen
可能是如何在MYSQL中使用相同ID的多行JOIN 2个表的重复问题。 - SnakeEye
请提供每个表的样本数据,获取的数据以及您期望的结果。 - Viki888
3个回答

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


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; 但它不返回任何内容。 - Chi Shen
你是否收到任何异常或没有得到任何结果? - Aravind Pillai

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

这可能会花费更长的时间,但最有可能会给您期望的结果。

祝你好运。


更长的是什么? - alirakiyan
@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

希望能对你有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接