0

我在 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?

我确定这与加入有关,但我不知道具体该怎么做?有什么建议么?

4

2 回答 2

3

经典的 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;
于 2013-02-28T01:55:07.980 回答
2

你可以做:

SELECT Questions.textofquestion
FROM EventType
   INNER JOIN EventAsks 
        ON EventType.etid = EventAsks.etid
   INNER JOIN Questions 
        ON EventAsks.qid = Questions.qid
WHERE EventType.etid = 1

由于您已经拥有etid=1yuou,因此可以简化为:

SELECT Questions.textofquestion
FROM EventAsks 
   INNER JOIN Questions 
        ON EventAsks.qid = Questions.qid
WHERE EventAsks.etid = 1
于 2013-02-28T01:56:17.913 回答