Hitesh Khunt
Hitesh Khunt

Reputation: 133

Query Issue for joining 2 tables

This Query is not Run.. Please solve this problem..

select q.id,q.name,qq.text 
from quiz q,quiz_question qq 
where qq.quiz_id='1'

table:

quiz
   id,name
quiz_question
   id,quiz_id,text

Upvotes: 0

Views: 73

Answers (4)

Kao
Kao

Reputation: 2272

What you need to do is join the tables together, on the quiz_id.

This can be done as:

 SELECT
  q.id,q.name,
  qq.text
 FROM
  quiz q
  INNER JOIN quiz_question qq ON ( q.id = qq.quiz_id )
 WHERE
  q.id = 1

Upvotes: 0

manurajhada
manurajhada

Reputation: 5380

select q.id,q.name,qq.text 
from quiz q inner join quiz_question qq on q.id = qq.quiz_id
where q.quiz_id='1';

OR

select q.id,q.name,qq.text 
from quiz q inner join quiz_question qq on q.id = qq.quiz_id and q.quiz_id='1';

Upvotes: 1

juergen d
juergen d

Reputation: 204854

Don't use legacy SQL syntax. If you want to join tables use an expiclit join

select q.id, q.name, qq.text 
from quiz q
inner join quiz_question qq on q.id = qq.quiz_id
where q.id = 1

Upvotes: 0

John Woo
John Woo

Reputation: 263803

There's is no problem with your query. I bet it will run but have wrong result. You are cross joining the table. I think you want:

select q.id, q.name, qq.text 
from   quiz q,quiz_question qq 
where qq.quiz_id = '1' AND
      q.id=qq.quiz_id

but better do this

select q.id, q.name, qq.text 
from   quiz q INNER JOIN quiz_question qq 
           on q.id=qq.quiz_id
where  qq.quiz_id = '1'

and one more thing, if your id is number then you shouldn't wrap it with single quote.

select q.id, q.name, qq.text 
from   quiz q INNER JOIN quiz_question qq 
           on q.id=qq.quiz_id
where  qq.quiz_id = 1

Upvotes: 2

Related Questions