[英]Mysql:one to many and php pagination
这是我的表结构
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行
如果我的查询只支持5行分页
我的阵列将在帖子2中丢失标签1中的标签
并且在第2页中仅显示丢失的标签
我该如何使用Perfect Pagination查询并获得完整数组
如何也优化我的查询
现在,您的SQL查询返回的内容如下:
<?php
$posts = array(
array ('post_id' => 1, 'user_id' => 1 ...),
array ('post_id' => 1, 'user_id' => 1 ...)
);
?>
每个“ catname”和“ tagname”列均带有一个新行。
我建议在构建php数组时使用单独的查询,例如:
SELECT * FROM posts
然后,对于帖子的每次迭代,您查询所需的数组,例如:
SELECT cats.cat_name
FROM post_relation, cats
WHERE post_relation.post_id = 1
AND relation_type = 'cat'
这将为您的类别以及类似的标签获取数组。
MySQL没有实际在列中返回数组的方法,如果您真的想通过单个查询完成所有操作,则可能必须使用子查询进行CONCAT,然后让您的php使用CSV或其他方式将结果拆分为数组。 不推荐两种方式。
由于我认为您不能直接从mysql结果中获取多维数组,而是使用一些php代码购买
首先获取帖子,然后您可以在其中循环播放,通过evry post_id,您可以为每个帖子获取标签和猫作为数组,并将其与posts数组一起放置
她是代码$ posts_array = array(); $ posts_per_page = 5; //您将从分页功能中获取//删除所有帖子
$posts_result = mysql_query("select * from posts ");
while ($post = mysql_fetch_assoc($posts_result)) {
// retreiving cats for evry post
$post_cat_q = 'SELECT cats.cat_name
FROM post_relation, cats
WHERE post_relation.post_id = '.$post['post_id'].'
AND post_relation.with_id = cat.cat_id
AND post_relation.relation_type = "cat"';
$post_cat = mysql_query($post_cat_q);
while($cat = mysql_fetch_assoc($post_cat)){
// loop in cats' rows to make an array and link it with the post array
$post['cats'][] = $cat;
}
// retreiving tags for evry post
$post_tags_q = 'SELECT tags.tag_name
FROM post_relation, tags
WHERE post_relation.post_id = '.$post['post_id'].'
AND post_relation.with_id = tags.tag_id
AND post_relation.relation_type = "tag"';
$post_tags = mysql_query($post_tags_q);
while($tag = mysql_fetch_assoc($post_tags)){
// loop in tags' rows to make an array and link it with the post array
$post['tags'][] = $tag;
}
$posts_array[] = $post;
}
echo '<pre>';
// ptinting the resulted array
print_r($posts_array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.