cute_programmer
cute_programmer

Reputation: 372

SQL/MYSQL : Selecting data in a junction table

I just want to ask, I'm not that good doing database query specially using junction table in a many to many schema.

First I have a table of blogs :

blog_table

 | blogId | title        | content |
 | cpi123 | cheal travel | blah....

then I have also a keywords table

keywords_table

 | keywordId | keyword |
 | log21     | cheap   |

I use junction table because I realize its a many to many relationship. One keyword can connect to many blogs and one blogs can have many keywords. So I use junction table for my blog search

blog_search

 | searchId | blogId | keywordId |
 | shak21   | cpi123 |  log21    | 

My goal is I want to retrieve the blog which correspond to the searched keyword (the keyword column in the keywords_table) so if i searched "cheap" the bog "cheap travel" should be retrieved.

Thanks in advance

Upvotes: 0

Views: 973

Answers (1)

Prabhath Amaradasa
Prabhath Amaradasa

Reputation: 503

You can use JOIN

SELECT
bt.title , kt.keyword
FROM blog_table as bt 
INNER JOIN blog_search   AS bs ON bt.blogId  = bs.blogId 
INNER JOIN keywords_table  AS kt ON  bs.keywordId  = kt.keywordId
WHERE  kt.keyword  ='cheap'

Upvotes: 2

Related Questions