【问题标题】:how to get tags matching with posts id and display records如何获取与帖子ID匹配的标签并显示记录
【发布时间】:2018-01-18 08:10:25
【问题描述】:

伙计们,我有以下表格

  1. 帖子
  2. 标签
  3. post_tags

我将post_tags表中的postID和tagsID保存为post_id,tag_id

现在我希望在我的 post.php 页面中显示与标签匹配的帖子,但是如何进行查询以获取匹配的标签?

$stmt = $db->query('SELECT * FROM posts WHERE postID LIKE "%'.$id.'%"');                
                $stmt->execute();
                while($rw = $stmt->fetch()){

}

我在这里分享我的实际数据结构以及数据是如何保存到每个表中的,希望这能给你一个想法来帮助我处理 JOIN 表?

这是 3 张桌子

--
-- Table structure for table `blog_posts`
--

CREATE TABLE IF NOT EXISTS `blog_posts` (
  `postID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `posterID` int(11) NOT NULL,
  `catID` int(11) NOT NULL,
  `postTitle` varchar(90) DEFAULT NULL,
  `postImg` varchar(255) DEFAULT NULL,
  `postYoutube` varchar(100) DEFAULT NULL,
  `postSlug` varchar(255) DEFAULT NULL,
  `postDesc` text,
  `postCont` text,
  `postViews` int(11) NOT NULL DEFAULT '0',
  `postDate` datetime DEFAULT NULL,
  PRIMARY KEY (`postID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `blog_posts`
--

INSERT INTO `blog_posts` (`postID`, `posterID`, `catID`, `postTitle`, `postImg`, `postYoutube`, `postSlug`, `postDesc`, `postCont`, `postViews`, `postDate`) VALUES
(1, 1, 1, ' Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.', ' Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.jpg', '', '1/lorem-ipsum-giving', '<p> Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', '<p> Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', 1, '2017-08-10 11:46:36'),
(2, 1, 3, ' Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.', ' Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.jpg', '', '2/lorem-ipsum-giving', '<p>Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', '<p>Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', 0, '2017-08-10 13:38:57'),
(3, 1, 2, 'Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.', 'Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.jpg', '', '3/moong-dal-halwa', '<p>Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', '<p>Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.</p>', 0, '2017-08-10 13:39:40'));

-- --------------------------------------------------------

--
-- Table structure for table `blog_posts_tags`
--

CREATE TABLE IF NOT EXISTS `blog_posts_tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `post_id` int(11) NOT NULL,
  `tag_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `blog_posts_tags` (`post_id`,`tag_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;

--
-- Dumping data for table `blog_posts_tags`
--

INSERT INTO `blog_posts_tags` (`id`, `post_id`, `tag_id`) VALUES
(1, 1, 2),
(7, 1, 4),
(9, 1, 3),
(2, 1, 3),
(10, 1, 2),
(8, 1, 5),
(4, 1, 5),
(24, 2, 2),
(25, 3, 5),
(26, 3, 1));

-- --------------------------------------------------------

--
-- Table structure for table `blog_tags`
--

CREATE TABLE IF NOT EXISTS `blog_tags` (
  `tagID` int(11) NOT NULL AUTO_INCREMENT,
  `tagName` varchar(32) NOT NULL,
  `tagUrl` varchar(32) NOT NULL,
  PRIMARY KEY (`tagID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=61 ;

--
-- Dumping data for table `blog_tags`
--

INSERT INTO `blog_tags` (`tagID`, `tagName`, `tagUrl`) VALUES
(1, 'American Food', 'american-food'),
(2, 'Bakeries', 'bakeries'),
(3, 'Bars', 'bars'),
(4, 'Beef', 'beef'),
(5, 'Cafe', 'cafe');

有什么帮助吗?

【问题讨论】:

    标签: mysql pdo


    【解决方案1】:

    你需要使用JOIN,例如:

    SELECT *
    FROM blog_posts bp JOIN blog_posts_tags bpt ON bp.postID = bpt.post_id
    JOIN blog_tags bt ON bt.tagID = bpt.tag_id
    WHERE bt.tagName = 'Bakeries';
    

    如果要通过id搜索,可以在WHERE条件下使用t.id,例如:

    SELECT *
    FROM blog_posts bp JOIN blog_posts_tags bpt ON bp.postID = bpt.post_id
    JOIN blog_tags bt ON bt.tagID = bpt.tag_id
    WHERE bt.tagID = 2;
    

    这里,id对应于post和tag的id(在各自的表中),post_idtag_idpost_tags表中对应外键列的名称。

    这是SQL Fiddle

    【讨论】:

    • 我是这个 JOIN 的新手,请回答我的问题 :) 说我有一个变量 id 为 20 现在我如何显示帖子表中的所有记录,这些记录的标签存储在 post_tags 中,tag_id 为 20?
    • 在这种情况下,您只需将WHERE 条件更改为WHERE t.id = 20;
    • 我试过这样但没有记录显示 $stm = $db->query('SELECT * FROM blog_posts p JOIN blog_post_tags pt ON p.id = pt.post_id JOIN blog_tags t ON t.tagID = pt.tag_id WHERE t.tagID = "'.id.'"');
    • 在 MySQL admin 中尝试该查询,看看它是否返回任何结果?也许没有记录,或者您通过post id 而不是tag id
    • 我在表 post_tags tag_id 中有 3 条记录,标签表的 tagID 为 20
    猜你喜欢
    • 2016-04-05
    • 2020-06-17
    • 2021-02-10
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多