Reputation: 155
This is table topics
:
id
label
This is table articles
:
id
label
content
topic_id
(foreign key)This is table comments
:
id
content
article_id
(foreign key)Now, I want to implement a search function that yields all articles where the search term (LIKE %term%) is included either in
label
or content
label
of the article’s topiccontent
of comments that belong to this articleI think the solution may have something to do with JOINS, but I absolutely have no idea how to do this. Can anybody help?
Any help is greatly appreciated!
Upvotes: 0
Views: 26
Reputation: 48780
I guess you are a newbie and don't want to implement a "Full Text Search" solution. Therefore, a simple SQL would be:
select a.*
from topics t
join articles a on a.topic_id = t.id
join comments c on c.article_id = a.id
where a.label like '%term%'
or a.content like '%term%'
or t.label like '%term%'
or c.content like '%term%'
Please note, the performance of this SQL solution is not great when dealing with a high volume or topics, articles or comments. However, it will work well and will return the articles you want.
Upvotes: 1