【问题标题】:help getting products + their comments by followers with mysql使用 mysql 帮助获取产品 + 关注者的评论
【发布时间】:2011-07-07 21:00:32
【问题描述】:

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

社交

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

好的,问题是

我们如何获取他们的关注者及其关注的 cmets 的产品列表(最多 20 个 cmets)?

这是我到目前为止所拥有的(没有 cmets),例如 $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)

谢谢!

【问题讨论】:

  • 我不久前曾问过类似的问题 (stackoverflow.com/questions/2856682/…),大家一致认为将 PHP 与 MySQL 混合可能会更容易。因为在一个查询中可能会变得如此复杂,以至于执行多个查询要干净得多。我最终沿着这条路走,我很高兴我做到了。 YMMV。
  • 是的,可以,问题是我对这里的逻辑感到困惑......不知何故,现在我可以做类似 listProductsCommentsFromFollower 的事情。我现在正在尝试混合这两个查询,以便它可以像上面一样制作护套。
  • 在这种情况下我看到的多个查询的问题是嵌套的 cmets。所以我的问题是,为什么你需要一次所有产品的所有 cmets?您有一个列出所有产品、关注者数量和每条评论的页面?或者更现实地说,您是否有一个列出所有产品、总关注者和总 cmets 的页面?
  • @kevin 不,我们没有,我想我们可以通过 sql 限制它们的位置。我还在上面。
  • 感谢您的回复,但这并不能真正回答我的问题。我正在寻找您的用例(您在用这些数据做什么?)。只有这样,我才能帮助您找到执行它的最佳方法。

标签: php mysql architecture


【解决方案1】:

您无法从查询中得到想要的结果。所以有两种选择。照常运行查询并遍历每个产品以获取它们的 cmets。

否则会得到一个包含所有 cmets 的大结果,然后它们处理数组以提取 cmets。

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

无论如何,这里有一个示例,说明如何使用 cmets 的 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;
}

现在您应该拥有与以前相同的数组,但现在数组中的每个元素都有一个数组,其中包含 cmets 的键 cmets。

【讨论】:

  • 如果他们需要所有数据,这就是我可能要走的路线......这看起来很奇怪。我看到的唯一问题是,您需要根据他的maybe it should be something like 示例,递归地将作为其他 cmets 子级的 cmets 添加到父级下的数组中。
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 2013-12-28
相关资源
最近更新 更多