我在MySql中有三个表。表1具有以下字段

表格1:

EventType
-etid
-description


表2:

EventAsks
-etid
-qid


表3:

Questions
-qid
-textofquestion


如果我有一个特定的etid,并且想查找该事件要求的所有问题。因此,鉴于下表...

EventType
etid     description
1        hiking
2        biking


EventAsks
etid      qid
1         1
1         3
2         2
2         3

Questions
qid   textofquestion
1     Is it fun?
2     Is it lots of exercise
3     Is it expensive


因此,给定的etid的结果为etid = 1,我希望返回与etid = 1相关的问题...

Result
Is it fun?
Is it expensive?


我确定这与联接有关,但是我不知道该怎么做?有什么建议么?

最佳答案

经典的N对N关系:

SELECT Questions.textofquestion FROM EventType
LEFT JOIN EventAsks ON EventAsks.etid = EventType.etid
LEFT JOIN Questions ON Questions.quid = EventAsks.qid
WHERE EventType.etid = 1;

关于mysql - INNER JOIN的SQL语法问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15126098/

10-10 06:38