我有3张桌子; events,memberEvents和members。
事件:eventId
,eventName
,eventDivision
memberEvents:memberID
,eventOne
,eventTwo
,eventThree
,eventFour
,eventFive
成员:memberID
,memberFirstName
,memberLastName
我试图让它显示events
。eventName
后跟memberFirstName
&正在进行该活动的memberLastName
名成员
这是我一直在尝试的查询:
SELECT * FROM
events
,memberEvents
,members
WHERE
events
。eventDivision
='C'正确加入
memberEvents
。memberID
ON
events
。eventID
=memberEvents
。eventOne
或
events
。eventID
=memberEvents
。eventTwo
或
events
。eventID
=memberEvents
。eventThree
或
events
。eventID
=memberEvents
。eventFour
或
events
。eventID
=memberEvents
。eventFive
当我运行时,我得到“#1066 - 不唯一的表/别名:'memberEvents'”
答案 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语法。
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';
也许你有充分的理由选择你所选择的表结构,但它是一个非规范化的设计,如果你需要增加每个成员的事件数量,你需要修改你的模式和代码以适应。