【发布时间】:2018-01-18 08:10:25
【问题描述】:
伙计们,我有以下表格
- 帖子
- 标签
- 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');
有什么帮助吗?
【问题讨论】: