Select multiple value using subquery

65 Views Asked by At

I have three tables:

  1. articles

  2. articleTags

  3. article_articleTags_Rel

articleTags_Rel table has 2 columns

  1. ArticleID_FK

  2. TagID_FK

As you can see the article_articleTags_Rel is to make relation between the articles and articleTags

All I want is to select one row from the articles table and and all related tags (multiple values) using sub-query.

How can I achieve this?

2

There are 2 best solutions below

2
On BEST ANSWER

You can try something like this:

select a.name, b.TagName
from 
  article_articleTags_Rel c
  inner join articles a on a.ID = c.ArticleID_FK
  left outer join articleTags b on b.ID = c.TagID_FK

Result:

name    TagName
---------------
art A   tag 1
art A   tag 2
art A   tag 3
art B   tag 1
art B   tag 3
art C   tag 1
art C   tag 3

sql fiddle: http://sqlfiddle.com/#!6/8d30f/2

1
On

Since you are not providing any sample, I guess you need only a brief idea:

  SELECT a.*, t.* FROM Articles a
  JOIN article_articleTags_Rel r
  ON a.ArticleID = r.ArticleID_FK
  JOIN articleTags t
  ON t.TagID = r.TagID_FK