bmeyer
February 7th, 2008, 05:10 PM
I'm trying to write a search function in PHP and can't get my SQL statements to work properly. Here's the situation:
1 table contains posts and another contains comments made on a post. When a search occurs, I'd like it to search posts.title and comments.text. Obviously, a post can have more than one comment in the db. When I do a query like:
SELECT posts.id FROM posts, comments WHERE post.title like '%TERM%' OR (comments.post=posts.id AND comments.text like '%TERM%')
the problem comes up. Single posts are returned multiple times, since each has many comments.
Is there a way to return a post's id ONCE if the search term is in the post title or a comment's text? Some sort of join function?
Thanks in advance for any help!
1 table contains posts and another contains comments made on a post. When a search occurs, I'd like it to search posts.title and comments.text. Obviously, a post can have more than one comment in the db. When I do a query like:
SELECT posts.id FROM posts, comments WHERE post.title like '%TERM%' OR (comments.post=posts.id AND comments.text like '%TERM%')
the problem comes up. Single posts are returned multiple times, since each has many comments.
Is there a way to return a post's id ONCE if the search term is in the post title or a comment's text? Some sort of join function?
Thanks in advance for any help!