0
みんなのタグマッチングを取得するにはどうすれば持って、次の表投稿IDと表示レコードを
- 投稿
- タグ
- post_tags
私はポスト、タグからpostIDとtagsIDを保存post_tagテーブルのテーブルをpost_id、tag_id
ここで私のpost.phpページbu t一致するタグを取得するためにクエリを実行するにはどうすればよいですか?ここ
$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');
任意のヘルプですか?あなたはJOIN
を使用する必要が
私は20歳で変数idを持っていると言っています。投稿タグのある投稿テーブルのすべてのレコードを表示するには、posts_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ここでt.tagID = "'.id。'" '); –