开发者

Search Multiple Tables with SQL Server

开发者 https://www.devze.com 2023-01-27 13:32 出处:网络
I have three tables: ____________________________________________________________ posts开发者_如何学Python tagsposts_x_tags

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号