Lets take stackoverflow as an example:
Post
Postid Title Mess
--------------------------------------------------
1 Title1 This is a question
2 Title2 This is a question1
3 Title3 This is a question2
4 Title4 This is a question3
5 Title5 This is a question4
6 Title6 This is a question5
Tags
TagId PostId Name
-----------------------------
1 1 Tag1
2 1 Tag2
3 1 Tag3
4 1 Tag4
5 2 Tag5
6 3 Tag6
7 4 Tag7
8 5 Tag8
9 6 Tag9
10 3 Tag10
In this design how would I get all the questions with its associated tags in one query. is this even possible?
*Edit*
select t.*, p.* from Tags t
join post p on t.postid=p.postid
Something like this would give me all the tags for each question. But I don’t think this is efficient. What do you think.
I think that your example is very weak. You shouldn’t have the tag name in the table you use to join posts and tags.
You should have the following tables:
If you don’t do this then you’ll have many equal tag names when 2 posts have the same tag, do you see?
Now, in order to query these tables and get all questions with the associated tags you should do the following:
You can say it is less efficient but you’re not considering data duplication in your example, which may cause serious errors.