1

出于某种原因,当我运行此查询时,它会两次返回相同的数据(双倍)。数据只在数据库中一次。为什么它返回双打?我想不通。

注意:我是这类查询的新手,因此非常感谢任何指导。

SELECT 
             `conversations_messages`.`message_date`,
             `conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
             `conversations_messages`.`message_text`,
             `users`.`username`
      FROM   `conversations_messages`
      INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
      INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
      WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
      ORDER BY `conversations_messages`.`message_date` DESC

.....这是完整的功能。

function fetch_conversation_messages ($conversation_id) {
$conversation_id=(int)$conversation_id;
$sql="SELECT 
             `conversations_messages`.`message_date`,
             `conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
             `conversations_messages`.`message_text`,
             `users`.`username`
      FROM   `conversations_messages`
      INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
      INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
      WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
      ORDER BY `conversations_messages`.`message_date` DESC";

     $result = mysqli_query($sql);
     $messages = array();

     while (($row= mysqli_fetch_assoc($result)) !== false){
       $messages[] = array(
            'date'      => $row['message_date'],
            'unread'    => $row['message_unread'],
            'text'      => $row['message_text'],
            'username'  => $row['username'],
       );
}
     return $messages;

}

4

1 回答 1

2

您的查询正在创建笛卡尔积。我假设每条消息有两个对话成员,并且加入匹配他们两个。

于 2013-04-14T00:37:48.657 回答