简体   繁体   中英

Mysql query returning too many rows

so I have 3 tables all inner joined, I am trying to put discussions with the discussion topic on my main page, with the users name and the latest discussion message on the main page as well.

THE PROBLEM: The query is returning all messages attached to all the discussions.....I just need the last message associated with the discussion returned.

I have been at this all day and cant figure it out. I have tried group by but that just gave me an error on mysqli_fetch_array

Here is my code

$discussionquery = "SELECT discussion_messages.discussion_id, user_profile.first_name, user_profile.last_name, discussion_messages.message_text, case_discussion.discussion_title 
                        FROM `user_profile` 
                        INNER JOIN discussion_messages 
                        ON (user_profile.user_id = discussion_messages.user_id) 
                        INNER JOIN case_discussion 
                        ON (case_discussion.discussion_id = discussion_messages.discussion_id) 
                        WHERE discussion_messages.case_id = '$thecaseid' 
                        ORDER BY discussion_messages.message_id DESC";
    $discussionquerydata = mysqli_query($dbc, $discussionquery); 

    while ($row2 = mysqli_fetch_array($discussionquerydata)) {
    $discussion_id = $row2['discussion_id'];

    ?>
    <!-- Begin Recent Discussions -->  
    <div class="row">
      <a href="discussion.php?newfact=$thecaseid'>">
        <div class="col-xs-4 col-md-2">
          <img src="img/client4.jpg" class="profile_picture img-rounded">
          <?php echo '<h6 class="discussionname">' . $row2['first_name'] . ' ' . $row2['last_name'] . '</h6>';?>
        </div>
      </a>
      <div class="col-xs-8 col-md-10 discussion_title">
         <?php echo '<h5><a href="discussion.php?caseid='.$thecaseid.'&did='.$discussion_id.'"> '.$row2['discussion_title'].'</a> -'. $row2['message_text'];?></h5>           
        <h6 class="pull-right">Dec. 25</h6>
      </div>
    </div>
    <?php
    };

One suggestion, use table aliases. The amount of total code will be smaller and easier to understand. Use the limit clause to return 1 row.

$discussionquery = "
 SELECT 
   m.discussion_id, 
   u.first_name, 
   u.last_name, 
   d.message_text, 
   c.discussion_title                         
 FROM `user_profile` as u                       
 INNER JOIN discussion_messages as m
   ON (u.user_id = m.user_id) 
 INNER JOIN case_discussion c
   ON (m.discussion_id = c.discussion_id) 
 WHERE 
   m.case_id = '$thecaseid' 
 ORDER BY 
   m.message_id DESC
 LIMIT 1";

You need logic to find the last message for each discussion. There is more than one way to write this condition.

The following does it using a not exists clause. This checks that there are no messages with a larger id for the discussion:

SELECT dm.discussion_id, up.first_name, up.last_name, dm.message_text, cd.discussion_title 
FROM `user_profile` up INNER JOIN
     discussion_messages dm
     ON (user_profile.user_id = dm.user_id) INNER JOIN
     case_discussion cd
     ON (cd.discussion_id = dm.discussion_id) 
WHERE dm.case_id = '$thecaseid' and
      not exists (select 1
                  from discussion_messages dm2
                  where dm2.discussion_id = dm.discussion_id and
                        dm2.message_id > dm.message_id
                )
ORDER BY dm.message_id DESC;

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