MySQL查询返回双精度数

时间:2013-04-14 00:29:44

标签: php mysql 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

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

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;

}

1 个答案:

答案 0 :(得分:2)

您的查询正在创建Cartesian product。我假设每条消息都有两个会话成员,并且连接正在匹配它们。