Barret Wallace
Barret Wallace

Reputation: 155

Search over fields from related tables

This is table topics:

This is table articles:

This is table comments:

Now, I want to implement a search function that yields all articles where the search term (LIKE %term%) is included either in

I 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

Answers (1)

The Impaler
The Impaler

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

Related Questions