Click here to Skip to main content
15,889,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following query to get the all questions from the database and the separate query to get tags for a specific question.

FYI, I have 3 tables.

Question
Tag
QuestionTagMap

Select QuestionId, Title, UrlSlug
    From dbo.Question

SQL
Select t.Id ,t.Name, t.UrlSlug
    from dbo.QuestionTagMap qtm
    Inner Join dbo.Tag t on qtm.Tag_Id = t.Id
    where qtm.Question_Id = 101


What I have tried:

But I want to write a query to get all questions and tags for each question.

How do I do that?
Posted
Updated 17-Apr-20 2:04am
Comments
Maciej Los 17-Apr-20 1:25am    
"I want to write a query to get all questions and tags for each question." - Can you be more specific?
Your query already doing it. What are you trying to achieve?
Sample data would be helpful.
MadMyche 17-Apr-20 8:05am    
The question table is not sourced at all, only the tag and bridge/map tables are being called

1 solution

To add in the question; you will need to add in your Question table to the FROM / INNER JOIN list "source" list.

To get All of the questions and tags, you will need to remove the WHERE clause from the query.

Without knowing the table structures, I can only guess as to the exact column names; so this most likely would need to be touched up
SQL
SELECT q.ID, q.QuestionText
     , t.Id ,t.Name, t.UrlSlug

FROM   dbo.Question             q
INNER JOIN dbo.QuestionTagMap qtm ON q.ID = qtm.Quest_ID
INNER JOIN dbo.Tag              t ON qtm.Tag_Id = t.Id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900