Mysql query me perplexed

New post As a result, I completely avoided this request. I simply could not get the results that I was looking for. To get the desired results, I came up with this ...

Also, if you guys came up with this query, I would really try to delete this work.

Thanks for all the help so far!

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;

}

Then, when I call this function on my 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'];

}

Original post I'm in trouble guys. See if you can help me put this request together.

I have a table called 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 ;

And a table called users.

    CREATE TABLE  `db.app`.`users` (
    `id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
    `nickname` VARCHAR( 64 ) NOT NULL ,
    PRIMARY KEY (  `id` )
    ) ENGINE = MYISAM ;

, messages. sender, 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  

....

+3
3

JOIN :

SELECT users.id, users.nickname, messages.body, messages.timestamp 
   FROM messages JOIN users ON messages.recipient = users.id

, , , ( ):

CREATE TABLE  `db.app`.`messages` (
  `id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
  `sender` INT( 32 ) NOT NULL ,
  `recipient` INT( 32 ) NOT NULL ,
  `body` TEXT NOT NULL ,
  `timestamp` INT( 32 ) NOT NULL ,
  PRIMARY KEY (  `id` )
) ENGINE = MYISAM ;
0

- ( 1, 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
0

Is this what you want:

SELECT users.id AS other_person_id, users.nickname AS other_person_nickname, messages.body AS last_message, messages.timestamp AS last_message_timestamp FROM messages LEFT JOIN users ON (messages.recipient = users.id) ORDER BY messages.id DESC LIMIT 1
0
source

Source: https://habr.com/ru/post/1776146/


All Articles