在Group by中获取最后一个ID MAX(message_id)

时间:2014-09-03 23:11:41

标签: php max

我正在尝试获取数据库中的最后一个ID,以便将新数据打印到我的div中。它不会使用message_id更新最新添加内容的新数据。最后一个查询工作并将前面的“新消息”插入到同一个div中,因此我认为它与我的第一个查询的结构有关。我使用MAX但它不起作用。

我非常确定,因为我将消息分组在一起,我需要获得每个组的最大ID,而不是完整的表。那我该怎么做呢?

  $user1_id= mysqli_real_escape_string($mysqli,$_SESSION['id']);
            $callmessage="SELECT MAX(message_id) AS message_id,message_content,message_target,message_status,message_creator,message_throughurl,message_time FROM messages WHERE message_target='$user1_id' OR message_creator='$user1_id' AND message_status='1' OR message_status='2'
            Group By 

           (if(message_target > message_creator,  message_creator,message_target))
           ,(if(message_creator > message_target,  message_target,message_creator))

            ORDER BY message_id DESC ";
            $chantmessage=mysqli_query($mysqli,$callmessage) or die(mysqli_error($mysqli));
            $nummessage=mysqli_num_rows($chantmessage);

            if($nummessage==0){
            echo"No Messages";
            }else{
            while($message=mysqli_fetch_array($chantmessage))
            {
            if($message['message_creator']==$_SESSION['id'] || $message['message_target']==$_SESSION['id']){
            ?>

            <div class="notif_uimessage">
            <div class="notif_textmessage">

            <div id="notif_actual_textmessage-<? echo $message['message_id'] ?>" class="notif_actual_textmessage">
            <?
            $string=$message['message_content'];
            $truncated = (strlen($string) > 30) ? substr($string, 0, 30) . '...' : $string;
            $data=rawfeeds_user_core::getuser($message['message_creator']);
            $data2=rawfeeds_user_core::getuser($message['message_target']);
             echo "<img border=\"1\" src=\"userimages/cropped".$message['message_creator'].".jpg?photo_time=" . time()."\" onerror='this.src=\"userimages/no_profile_img.jpeg\"' width=\"40\" height=\"40\"><br />".$data['fullname']." >>  ".$data2['fullname']."<a class='markasreadmessage' href='".$message['message_throughurl']." '>".$truncated." </a><br />";
            echo "<abbr class='timeago' title='".$message['message_time']."'>".$message['message_time']."</abbr> - ";

            $sqlsmessage=mysqli_query($mysqli,"select * from messages WHERE message_creator=".$message['message_creator']." AND message_target=".$message['message_target']." AND message_status='1'");
                    $messages_count=mysqli_num_rows($sqlsmessage);
                    if($messages_count==0)
                    {
                    echo'No New Messages';
                    }else if($message_count>0)
                    {   
                    echo '<span class="newmessagescount">'.$messages_count.' NEW</span>';
                    }
            ?>
            </div>
            </div>
            </div>
          <?  }}}?>

0 个答案:

没有答案