Search Multiple Tables with SQL Server
I have three tables:
____________________ ____________________ ____________________
posts 开发者_如何学Python tags posts_x_tags
____________________ ____________________ ____________________
| id | title | body | id | tag_name | post_id | tag_id
posts and tags have a many to many relationship. Is is possible to do a search for posts.body OR posts.title OR tags.tag_name.
This gets me close but returns so many duplicates:
SELECT * FROM posts p
INNER JOIN posts_x_tags x ON p.id = x.post_id
INNER JOIN tags t ON t.id = x.tag_id
AND t.tag LIKE '%a%'
OR p.title LIKE '%a%'
OR p.body LIKE '%a%';
Any help would be much appreciated.
You can use SELECT DISTINCT ... in order to remove duplicates from result set.
精彩评论