Jovan
Jovan

Reputation: 4794

One to many select statement

I have quizzes table with columns id, quiz_name, owner_user_id and second table called user_answer with columns id, quiz_id, user_id, answer_id

I want to select quiz name, quiz id, and all users who played particular quiz with the given id. Can I do this with one SQL statement?

Later I need to pass this result in JSON

Upvotes: 0

Views: 27

Answers (1)

Dusan Radovanovic
Dusan Radovanovic

Reputation: 1679

Yes, you can. You need to join tables on quiz_id and select everything you want. This is the code if you want quiz name, quiz id and user ids.

SELECT q.quiz_name, q.id, u.user_id 
from quizzes q join user_answer u on (q.id = u.quiz_id) 
where q.id = {yourID}

Upvotes: 1

Related Questions