0

这是我的表结构

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 进行查询并获取完整数组

如何优化我的查询

4

2 回答 2

0

因为我认为您不能直接从 mysql 结果中获取多维数组,而是使用一些 php 代码购买

首先获取帖子,然后您可以循环进入它,并且通过 evry post_id 您可以将标签和猫作为每个帖子的数组,并将其与帖子数组放在一起

她是代码 $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);
于 2013-04-05T03:06:17.923 回答
0

现在您的 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 或其他东西将结果拆分为一个数组. 不推荐任何一种方式。

于 2013-04-05T01:58:22.203 回答