Reputation: 3946
I have three tables in MySql. Table 1 has the following fields
Table1:
EventType
-etid
-description
Table 2:
EventAsks
-etid
-qid
Table 3:
Questions
-qid
-textofquestion
If I have a specific etid and I want to find all the questions that the event asks for. So given the following tables...
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
So the result of a given etid say etid=1, I would like the questions associated with etid=1 returned...
Result
Is it fun?
Is it expensive?
I am sure this had to do with a join, but I don't know exactly how to do it? Any suggestions?
Upvotes: 0
Views: 43
Reputation: 19539
Classic n-to-n relationship:
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;
Upvotes: 3
Reputation: 4094
You can do:
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
Since you already have the etid=1
yuou can simplify as:
SELECT Questions.textofquestion
FROM EventAsks
INNER JOIN Questions
ON EventAsks.qid = Questions.qid
WHERE EventAsks.etid = 1
Upvotes: 2