gpt4 book ai didi

php - Mysql查询让我不知所措

转载 作者:行者123 更新时间:2023-11-29 01:05:02 25 4
gpt4 key购买 nike

新帖子我最终完全避免了该查询。我只是无法得到我想要的结果。为了获得预期的结果,我想到了这个......

另外,如果你们想出那个问题,我真的很想删除这个变通方法。

感谢迄今为止的所有帮助!

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;

}

然后当我在我的 Controller 上调用该函数时..

//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 '&timestamp='.$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 中。 收件人、邮件正文和 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

对于我的生活,我无法弄清楚这个查询......

最佳答案

这样的事情应该有效(发件人的 ID 假定为 1,收件人的 ID 假定为 2):

SELECT users.id, users.nickname, messages.body, messages.timestamp
FROM messages
JOIN users ON messages.recipient = users.id
AND messages.sender = 1
AND messages.recipient = 2

关于php - Mysql查询让我不知所措,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4241901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com