php - 使用 mysql 帮助关注者获取产品 + 他们的评论

标签 php mysql architecture

好的,我有一张产品表。和评论表。一个用户表和一个社交表。

社交

SID AUID BUID
1   1    2 // user 1 follow 2
2   1    3 // user 1 follow 3
3   2    1

我们可以看到用户id 1关注了用户2和3

用户

   UID     NAME
   1       me
   2       imthenamefromuser2
   3       imthenamefromuser3

产品

PID    UID    NAME           TIMECREATE
1      2      user2product   12-04-2011
2      3      user3product   12-03-2011

评论 pcid =父评论id

CID    UID    PID    COMMENT                  PCID
1      1      2      comment on product2      NULL
2      2      2      another comment on p2    NULL
3      3      2      someoneelse on p2        NULL
4      1      2      who are you?             3
5      3      2      im user 3 dude           4
6      1      1      a new post on p2         NULL

好的问题是

我们如何获得他们关注者的产品列表以及他们的以下评论(最多 20 条评论)?

这是我到目前为止的内容(没有评论)让我们举个例子 $uid 是 1

function listFromFollower($uid){
    #here
    $data = $this->fetchAll("SELECT products.name AS product, users.name, products.pid, products.timecreate
    FROM products
    INNER JOIN users ON users.uid = products.uid 
    INNER JOIN social ON products.uid = social.buid
    WHERE social.auid = :uid", array( 'uid' => $uid));
    return $data;
}

它得到类似的东西

array
  0 => 
    array
      'product' => string 'user2product' (length=9)
      'name' => string 'imthenamefromuser2' (length=12)
      'pid' => string '1' (length=1)
      'timecreate' => string '2011-02-26 13:30:07' (length=19)
  1 => 
    array
      'product' => string 'user3product' (length=8)
      'name' => string 'imthenamefromuser3' (length=12)
      'pid' => string '2' (length=1)
      'timecreate' => string '2011-02-26 13:30:54' (length=19)

也许应该是这样的

array
  0 => 
    array
      'product' => string 'user2product' (length=9)
      'name' => string 'imthenamefromuser2' (length=12)
      'pid' => string '1' (length=1)
      'timecreate' => string '2011-02-26 13:30:07' (length=19)
     0 =>
       array
         'name'     => string ''
         'comment'  => string ''
     1 =>
       array
         'name'     => string ''
         'comment'  => string ''
         0 =>
            array
             'name'     => string 'a threaded comment'
             'comment'  => string ''
     untill20 =>
       array
         'name'     => string ''
         'comment'  => string ''
  1 => 
    array
      'product' => string 'user3product' (length=8)
      'name' => string 'imthenamefromuser3' (length=12)
      'pid' => string '2' (length=1)
      'timecreate' => string '2011-02-26 13:30:54' (length=19)

谢谢!

最佳答案

您无法让查询的结果如您所愿。所以有两种选择。按原样运行查询并遍历每个产品以获取他们的评论。

或者得到一个包含所有评论的大结果,然后他们处理数组以提取评论。

正如 Kevin Peno 所说,您需要所有这些数据有什么理由吗?而不仅仅是评论数?因为那将是一个没有处理的简单查询。

无论如何,这是一个示例,说明如何使用 LEFT JOIN 来执行第二个选项以进行评论:

function listFromFollower($uid){
    $rows = $this->fetchAll("
        SELECT products.name AS product, users.name, products.pid, 
            products.timecreate, commenters.name as commenter, comments.comment
        FROM products
        INNER JOIN users ON users.uid = products.uid 
        INNER JOIN social ON products.uid = social.buid
        LEFT JOIN comments ON comments.pid = product.pid
        INNER JOIN users AS commenters ON commenters.uid = comments.uid
        WHERE social.auid = :uid", array( 'uid' => $uid));

    $data = array();

    foreach($rows as $row) {
        // If we haven't added this product to data yet, add it.
        if (!isset($data[$row['pid']])) {
            $data[$row['pid']] = $row;
            // Tidy up the data, we don't want commenter and comment here
            unset($data[$row['pid']]['commenter']);
            unset($data[$row['pid']]['comment']);

            // Initialize an array for the comments
            $data[$row['pid']]['comments'] = array();
        }

        $data[$row['pid']]['comments'][] = array(
            'name' => $row['commenter'],
            'comment' => $row['comment']
        );
    }

    return $data;
}

现在您应该拥有与之前相同的数组,但现在数组中的每个元素还有一个包含包含评论的关键评论的数组。

关于php - 使用 mysql 帮助关注者获取产品 + 他们的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5178044/

相关文章:

PHP 如何获取带有类和命名空间路径的方法名称作为字符串?

mysql - SQL对多个组进行排名

php - 如果两个表值匹配,查看值是否存在?

php - 使用php将相似的数据插入到两个表中

database - 有经验的数据库架构师的话题

php - php中绑定(bind)的套接字是什么?

PHP/MySQL mysql_num_rows 不返回值

php - 将数据从一列移动到表

java - "Adopting MapReduce model"= 可扩展性的通用答案吗?

model-view-controller - MVC 模型 1 和模型 2 有什么区别?