【问题标题】:Mysql:one to many and php paginationMysql:一对多和php分页
【发布时间】:2013-03-27 07:26:04
【问题描述】:

这是我的表结构

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 Rows

如果我的查询仅使用 5 行进行分页

我的数组会丢失 在帖子 1 中标记 在帖子 2 中标记

在第 2 页 只会显示丢失的标签

如何使用 Perfect Pagination 进行查询并获取完整数组

如何优化我的查询

【问题讨论】:

    标签: php mysql mysqli pagination


    【解决方案1】:

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

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

    她是密码 $posts_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);
    

    【讨论】:

      【解决方案2】:

      现在您的 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什么的。不推荐任何一种方式。

      【讨论】:

        猜你喜欢
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 2013-05-25
        • 2018-01-12
        • 2011-04-14
        • 2011-03-06
        相关资源
        最近更新 更多