我试图在我的TAGS表中进行搜索,并获得POSTS表的结果。让我解释一下:
POSTS表
|id|post_id |title |content
|1 |post1111111|Title 1|.............
|2 |post2222222|Title 2|.............
|3 |post3333333|Title 3|.............
|4 |post4444444|Title 4|.............
标签表
|id|related_to |tags
|1 |post1111111|chicken
|2 |post1111111|dog
|3 |post2222222|cat
|4 |post2222222|fish
|5 |post3333333|fish
|6 |post4444444|fish
|7 |post4444444|fisher
|8 |post4444444|fishing
鱼
输入:
Results should be like:
Title 2 - ............. (post2222222)
Title 3 - ............. (post3333333)
Title 4 - ............. (post4444444)
我的代码是:
$row= $db->prepare("select * from tags WHERE tag LIKE ? COLLATE utf8_general_ci");
$row->execute(array("%$value%"));
$show= $row->fetchAll(PDO::FETCH_ASSOC);
$x = $row->rowCount();
if($x){
foreach($show as $list){
echo $list['related_to'].", ";
}
}
Output: post2222222, post3333333, post4444444, post4444444, post4444444,
True Output: post2222222, post3333333, post4444444
之后,将这些帖子以其内容排序为结果
我意识到我的代码是不完整和错误的,但我不知道如何继续。我知道太长了。如果你能帮忙,我将很高兴。
通过连接posts
表来获取文章内容。
SELECT p.*
FROM posts AS p
JOIN (
SELECT DISTINCT related_to
FROM tags
WHERE tag LIKE ? COLLATE utf8_general_ci
) AS t ON p.post_id = t.related_to