简体   繁体   English

MySQL查询返回太多行

[英]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. 因此,我内部共有3个表,我试图在主页上进行有关讨论主题的讨论,并在主页上进行用户名和最新讨论消息的讨论。

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 我已经尝试了group by,但是这给了我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. 使用limit子句返回1行。

$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. 下面使用一个not exists子句来做到这一点。 This checks that there are no messages with a larger id for the discussion: 这将检查是否没有讨论的ID较大的消息:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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