ここに私のテーブル構造があります
CREATE TABLE `cats` (
`cat_id` int(11) NOT NULL auto_increment,
`cat_name` varchar(50) NOT NULL,
`cat_status` tinyint(2) NOT NULL,
PRIMARY KEY (`cat_id`),
KEY `cat_name` (`cat_name`,`cat_status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `cats`
--
INSERT INTO `cats` VALUES (1, 'news', 1);
INSERT INTO `cats` VALUES (2, 'sports', 1);
INSERT INTO `cats` VALUES (3, 'political', 1);
INSERT INTO `cats` VALUES (4, 'Computer', 1);
-- --------------------------------------------------------
--
-- Table structure for table `posts`
--
CREATE TABLE `posts` (
`post_id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`post_title` varchar(150) NOT NULL,
`post_desc` text NOT NULL,
`post_time` int(10) NOT NULL,
`post_status` tinyint(1) NOT NULL,
PRIMARY KEY (`post_id`),
KEY `user_id` (`user_id`,`post_time`,`post_status`),
FULLTEXT KEY `description` (`post_desc`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `posts`
--
INSERT INTO `posts` VALUES (1, 1, 'Barcha vs R.madrid', 'Football Match Messi vs reonaldo', 1365122983, 1);
INSERT INTO `posts` VALUES (2, 1, 'Web Devlopment Basic', 'etc etc etc etc etc etc etc etc etc etc etc ', 1365122983, 1);
-- --------------------------------------------------------
--
-- Table structure for table `post_relation`
--
CREATE TABLE `post_relation` (
`post_id` int(11) NOT NULL,
`relation_type` varchar(10) NOT NULL,
`with_id` int(11) NOT NULL,
KEY `relation_type` (`relation_type`,`with_id`,`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `post_relation`
--
INSERT INTO `post_relation` VALUES (1, 'cat', 1);
INSERT INTO `post_relation` VALUES (1, 'cat', 2);
INSERT INTO `post_relation` VALUES (2, 'cat', 4);
INSERT INTO `post_relation` VALUES (1, 'tag', 1);
INSERT INTO `post_relation` VALUES (1, 'tag', 4);
INSERT INTO `post_relation` VALUES (1, 'tag', 5);
INSERT INTO `post_relation` VALUES (2, 'tag', 6);
-- --------------------------------------------------------
--
-- Table structure for table `tags`
--
CREATE TABLE `tags` (
`tag_id` int(11) NOT NULL auto_increment,
`tag_name` varchar(50) NOT NULL,
`tag_status` tinyint(2) NOT NULL,
PRIMARY KEY (`tag_id`),
KEY `tag_name` (`tag_name`,`tag_status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `tags`
--
INSERT INTO `tags` VALUES (1, 'football', 1);
INSERT INTO `tags` VALUES (2, 'usa', 1);
INSERT INTO `tags` VALUES (3, 'war', 1);
INSERT INTO `tags` VALUES (4, 'messi', 1);
INSERT INTO `tags` VALUES (5, 'ronaldo', 1);
INSERT INTO `tags` VALUES (6, 'php', 1);
SQLオンライン: http://sqlfiddle.com/#!2/9d20d/8
私は今、猫とタグを含む投稿を取得しようとしています
このSQLクエリで
SELECT
posts.*, cats.cat_name as catname , tags.tag_name as tagname FROM
post_relation INNER JOIN
posts ON ( post_relation.post_id = posts.post_id ) LEFT JOIN
cats ON ( post_relation.with_id = cats.cat_id and post_relation.relation_type = "cat" ) LEFT JOIN
tags ON ( post_relation.with_id = tags.tag_id and post_relation.relation_type = "tag" )
この配列のような結果が欲しい
$posts = array(
array(
"post_id" => 1 ,
"user_id" => 1 ,
"post_title" => "Barcha vs R.madrid" ,
"post_desc" => "Football Match Messi vs reonaldo" ,
"post_time"=> 1365122983 ,
"post_status" => 1 ,
"post_cats" => array(
"1" => "news" ,
"2" => "sports"
) ,
"post_tags" => array(
"1" => "football" ,
"4" => "messi" ,
"5" => "Ronaldo" ,
)
) ,
array(
"post_id" => 2 ,
"user_id" => 1 ,
"post_title" => "Web Devlopment Basic" ,
"post_desc" => "etc etc etc etc etc etc etc etc etc etc etc " ,
"post_time"=> 1365122983 ,
"post_status" => 1 ,
"post_cats" => array(
"4" => "Computer"
) ,
"post_tags" => array(
"6" => "php" ,
)
)
)
私はPHPでこの配列を行うことができます問題ありませんしかし
私の例では、投稿が2つしかありません
しかし、私のクエリは7行を返します
クエリがページネーションで 5 行のみで機能する場合
私の配列は、ポスト 2 のポスト 1 タグのタグを失います
ページ番号2には、失われたタグのみが表示されます
完全なページネーションでクエリを実行して完全な配列を取得するにはどうすればよいですか
私のクエリも最適化する方法