简体   繁体   中英

Add many-to-many select into existing query

I have a query set up to return comments given from one user to another.

Now we want to allow the ability to rate these comments.

I've added a new table that has 3 fields: comment_id, user_id, and score.

How can I grab an array of {user_id,score} for any comment fetched?

Will I need to loop through the comments after they are fetched and run a second query? This approach could result in adding several extra queries.

Can it be done with a single query?

Here's the function I have now:

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;
  }
}

If you for example wanted all {comment_id, user_id, score} associated with the comments of user 5, your query would involve an INNER JOIN and look something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM