Home > OS >  SQL one to many, best practice?
SQL one to many, best practice?

Time:06-06

Ive got an one to many relationship, like e.g. one question with many answers. If I now want to get the question based on any answer, as well as all answers for a question, whats best practice to do so? I thought of using the question's id as FK in the answers, but how do I then get all answers for a question? Do I have to somehow "reverse look up" all answers and check their question's FK or is there any better solution?

Ty for any feedback!

CodePudding user response:

I'd do this:

SELECT ... FROM answers WHERE question_id = ?

That is, you can select a set of rows based on a value they all have in the question_id column, which I suppose is the foreign key column that references the question they are all in answer to.

  • Related