0

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?

3
  • Please post the code that you tried, the one that gives the syntax error, otherwise we can only guess. Commented Feb 28, 2013 at 1:53
  • SELECT * FROM eventasks WHERE etid =1 INNER JOIN Questions LIMIT 0 , 30 Commented Feb 28, 2013 at 1:54
  • Your inner join is invalid as it's not specifying a constraint to show how the join should work; see Madbreaks answer for details how to do this. Commented Feb 28, 2013 at 1:56

2 Answers 2

3

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; 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.