将多对多选择添加到现有查询中

时间:2014-01-31 17:23:33

标签: mysql select many-to-many

我设置了一个查询来返回从一个用户到另一个用户的注释。

现在我们希望能够对这些评论进行评分。

我添加了一个包含3个字段的新表:comment_id,user_id和score。

如何获取任何评论的{user_id,score}数组?

在获取注释并运行第二个查询后,是否需要遍历注释?这种方法可能会导致添加几个额外的查询。

可以使用单个查询完成吗?

这是我现在的功能:

function getAllComments($args) {
  if(empty($_SESSION['user']['id'])) return false;
  $limit = 5;
  if(isset($args['limit'])) $limit = $args['limit'];
  $page = 1;
  if(isset($args['page'])) $page = $args['page'];
  $data = array();
  $offset = ($page-1)*$limit;

  $sql = "SELECT c.*,CONCAT(u1.firstName,' ',u1.lastName) AS owner,u1.title AS ownerTitle,CONCAT_WS(' ',u2.firstName,u2.lastName) AS senderName,u2.title AS senderTitle,a.name AS actionName,a.behavior 
    FROM comment AS c 
    JOIN user AS u1 ON c.recipient = u1.id
    JOIN user AS u2 ON c.sender = u2.id
    JOIN action AS a ON c.action = a.id
    WHERE c.type=1";

  if(isset($args['location'])) $sql .= " AND u1.location=?";
  $sql .= " ORDER BY date DESC";
  $sql .= " LIMIT ?";
  $sql .= " OFFSET ?";

  try {
    $db = DB::getInstance();
    $stmt = $db->dbh->prepare($sql);
    $n = 1;
    //THESE MUST STAY IN THE SAME ORDER TO MATCH THE ? PLACEHOLDERS
    if(isset($args['location'])) $stmt->bindValue(($n++), $args['location'], PDO::PARAM_INT);
    $stmt->bindValue(($n++), $limit, PDO::PARAM_INT);
    $stmt->bindValue(($n++), $offset, PDO::PARAM_INT);
    $result = $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $rows = array();

    if($result !== false) {
      while($row = $stmt->fetch()) {
        $rows[] = $row;
      }
      $data['comments'] = $rows;
      return $data;
    }else {
      logit('Query Failed');
      return false;
    }
  }catch(PDOException $e) {
    logit($e->getMessage());
    return false;
  }
}

1 个答案:

答案 0 :(得分:0)

例如,如果您想要与用户5的评论相关联的所有{comment_id,user_id,score},则您的查询将涉及INNER JOIN,并且类似于:

SELECT s.comment_id, s.user_id, s.score FROM CommentScore s
INNER JOIN Comment c
ON c.comment_id = s.comment_id
WHERE c.user_id = 5