新文章 我最终避免使用那个查询。我无法得到我想要的结果。为了获得所需的结果,我想出了这个...
此外,如果你们能想出那个查询,我真的希望能够去掉这个解决方法。
非常感谢迄今为止所有的帮助!
function get_converstations($data)
{
//get possible senders into an array
$sender_sql = "SELECT DISTINCT `users`.`aid` AS `aid`, `users`.`nickname` AS `nickname`
FROM `users`,`messages`
WHERE `messages`.`sender` = '".$data['aid']."'
AND `messages`.`recipient` = `users`.`aid`";
$query = mysql_query($sender_sql);
if(mysql_num_rows($query) > 0)
{
$sender_data = array();
while ($row = mysql_fetch_array($query)){
$sender_data[$row['aid']] = $row['nickname'];
}
}
//get possible recipients into an array
$recipient_sql = "SELECT DISTINCT `users`.`aid`, `users`.`nickname`
FROM `users`,`messages`
WHERE `messages`.`recipient` = '".$data['aid']."'
AND `messages`.`sender` = `users`.`aid`";
$query = mysql_query($recipient_sql);
if(mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_array($query)){
$recipient_data[$row['aid']] = $row['nickname'];
}
}
//merge the arrays to overrite any duplicate people.
$no_keys_persons = array_merge($sender_data, $recipient_data);
//create a new array with keys
foreach($no_keys_persons as $aid => $nickname)
{
$persons[] = array(
"aid" => $aid,
"nickname" => $nickname
);
}
//print_r($persons);
//create the conversations array
foreach($persons as $person)
{
$sql = "SELECT * FROM `messages` WHERE `sender` = '".$data['aid']."' AND `recipient` = '".$person['aid']."' OR `sender` = '".$person['aid']."' AND `recipient` = '".$data['aid']."' ORDER BY `id` DESC LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_array($query))
{
$conversations[] = array(
"person_aid" => $person['aid'],
"person_nickname" => $person['nickname'],
"sender" => $row['sender'],
"recipient" => $row['recipient'],
"body" => $row['body'],
"timestamp" => $row['timestamp'],
"ip" => $row['ip']
);
}
}
}
//print_r($conversations);
return $conversations;
}
然后当我在我的控制器上调用该函数时...
//create the data array from url
$data = array(
"aid" => $_GET['aid'],
"nickname" => $_GET['nickname'],
"ip" => $_SERVER['REMOTE_HOST'],
);
//instantiate any classes
include 'db.php';
include 'messages_model.php';
$messages = new messages_model();
$coversations = $messages->get_converstations($data);
foreach ($coversations as $conversation)
{
echo '&conversation=';
echo '&sender='.$conversation['sender'];
if($conversation['sender'] === $data['aid']) { echo '&sender_nickname='.$data['nickname']; } else { echo '&sender_nickname='.$conversation['person_nickname']; }
echo '&recipient='.$conversation['recipient'];
if($conversation['recipient'] === $data['aid']) { echo '&recipient_nickname='.$data['nickname']; } else { echo '&recipient_nickname='.$conversation['person_nickname']; }
echo '&body='.$conversation['body'];
echo '×tamp='.$conversation['timestamp'];
}
原始信息 我很困惑。请看看能否帮我组合这个查询。
我有一张名为messages的表。
CREATE TABLE `db.app`.`messages` (
`id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
`sender` VARCHAR( 64 ) NOT NULL ,
`recipient` VARCHAR( 64 ) NOT NULL ,
`body` TEXT NOT NULL ,
`timestamp` INT( 32 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
还有一个名为“users”的表。
CREATE TABLE `db.app`.`users` (
`id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
`nickname` VARCHAR( 64 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
当一条消息被创建时,它会将发送者的ID插入到
messages
.sender
中,将接收者的ID插入到messages
.recipient
中,并插入消息正文和UNIX时间戳。这部分已经正常运作。我的问题在于如何获取所有唯一对话的列表(就像iPhone上的短信一样)。
因此,如果我们有以下数据...
messages table;
id | sender | recipient | body | timestamp
1 | 1234 | 5678 | testing message | 1290233086
2 | 5678 | 1234 | testing reply | 1290233089
users table;
id | nickname
1234 | john
5678 | peter
我希望能够生成以下格式的查询结果...
results;
other_person_id | other_person_nickname | last_message | last_message_timestamp
1234 | john | testing reply | 1290233089
我真的无法理解这个查询语句...