这是我的表结构
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);
SQLonline: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 Rows
如果我的查询仅使用 5 行进行分页
我的数组将丢失帖子 1 中的标签 帖子 2 中的标签
并且在第 2 页将仅显示丢失的标签
如何使用 Perfect Pagination 进行查询并获取完整数组
如何优化我的查询