MYSQL RIGHT JOIN

时间:2011-12-30 05:19:55

标签: mysql join

我有3张桌子; events,memberEvents和members。
事件:eventIdeventNameeventDivision
memberEvents:memberIDeventOneeventTwoeventThreeeventFoureventFive
成员:memberIDmemberFirstNamememberLastName

我试图让它显示eventseventName后跟memberFirstName&正在进行该活动的memberLastName名成员

这是我一直在尝试的查询:

  

SELECT * FROM eventsmemberEventsmembers

     

WHERE eventseventDivision ='C'

     

正确加入memberEventsmemberID

     

ON eventseventID = memberEventseventOne

     

eventseventID = memberEventseventTwo

     

eventseventID = memberEventseventThree

     

eventseventID = memberEventseventFour

     

eventseventID = memberEventseventFive   

     
    
      


当我运行时,我得到“#1066 - 不唯一的表/别名:'memberEvents'”       

    
  

3 个答案:

答案 0 :(得分:1)

尝试:


SELECT ev.*,  me.* FROM events ev
RIGHT JOIN memberEvents me
ON (ev.eventID = me.eventOne
OR ev.eventID = me.eventTwo
OR ev.eventID = me.eventThree
OR ev.eventID = me.eventFour
OR ev.eventID = me.eventFive)
WHERE ev.eventDivision = 'C'

答案 1 :(得分:0)

我认为你应该仔细看看防守

JOIN Syntax

您似乎误解了JOIN语法。

SELECT  * 
FROM    table1 t1 right join 
        table2 t2   on  t1.key1 = t2.key1 
                    and t1.key2 = t2.key2 
where   t1.somecolumn = somevalue

答案 2 :(得分:0)

您是否特别希望将每位成员的活动数量限制为五个?为什么不让一个memberEvent表具有由成员和事件的外键组成的主键?

memberEvent:memberId,eventId

然后你的查询将是

SELECT
event.eventName,
member.memberFirstName,
member.memberLastName 
FROM
event
INNER JOIN
memberEvent
ON
memberEvent.eventID = event.eventId
INNER JOIN
member
ON
memberEvent.memberId = member.memberId
WHERE
event.division = 'C';

也许你有充分的理由选择你所选择的表结构,但它是一个非规范化的设计,如果你需要增加每个成员的事件数量,你需要修改你的模式和代码以适应。