Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql Query has me at a loss
    primarykey
    data
    text
    <p><strong>New Post</strong> I ended up avoiding that query entirely. I just couldn't get the results I was looking for. To get the desired results, I came up with this...</p> <p>Also, if you guys come up with that query, I'd really look to remove this work around.</p> <p>Thanks for all the help so far!</p> <pre><code>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) &gt; 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) &gt; 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 =&gt; $nickname) { $persons[] = array( "aid" =&gt; $aid, "nickname" =&gt; $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) &gt; 0) { while($row = mysql_fetch_array($query)) { $conversations[] = array( "person_aid" =&gt; $person['aid'], "person_nickname" =&gt; $person['nickname'], "sender" =&gt; $row['sender'], "recipient" =&gt; $row['recipient'], "body" =&gt; $row['body'], "timestamp" =&gt; $row['timestamp'], "ip" =&gt; $row['ip'] ); } } } //print_r($conversations); return $conversations; } </code></pre> <p>Then when I call that function on my controller..</p> <pre><code>//create the data array from url $data = array( "aid" =&gt; $_GET['aid'], "nickname" =&gt; $_GET['nickname'], "ip" =&gt; $_SERVER['REMOTE_HOST'], ); //instantiate any classes include 'db.php'; include 'messages_model.php'; $messages = new messages_model(); $coversations = $messages-&gt;get_converstations($data); foreach ($coversations as $conversation) { echo '&amp;conversation='; echo '&amp;sender='.$conversation['sender']; if($conversation['sender'] === $data['aid']) { echo '&amp;sender_nickname='.$data['nickname']; } else { echo '&amp;sender_nickname='.$conversation['person_nickname']; } echo '&amp;recipient='.$conversation['recipient']; if($conversation['recipient'] === $data['aid']) { echo '&amp;recipient_nickname='.$data['nickname']; } else { echo '&amp;recipient_nickname='.$conversation['person_nickname']; } echo '&amp;body='.$conversation['body']; echo '&amp;timestamp='.$conversation['timestamp']; } </code></pre> <p><strong>Original Post</strong> I am at a loss here guys. Please see if you can help me put this query together.</p> <p>I have a table called messages.</p> <pre><code> 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 ; </code></pre> <p>And a table called users.</p> <pre><code> CREATE TABLE `db.app`.`users` ( `id` INT( 32 ) NOT NULL AUTO_INCREMENT , `nickname` VARCHAR( 64 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; </code></pre> <p>When a message is made, it inserts the ID of the sender into <code>messages</code>.<code>sender</code>, the ID of the recipient into <code>messages</code>.<code>recipient</code>, the message body, and UNIX timestamp. This is working fine.</p> <p>My problem lies with getting a list of all the unique conversations. (Like text messages on iPhones).</p> <p>so if we have data like so... </p> <pre><code>messages table; id | sender | recipient | body | timestamp 1 | 1234 | 5678 | testing message | 1290233086 2 | 5678 | 1234 | testing reply | 1290233089 </code></pre> <p></p> <pre><code>users table; id | nickname 1234 | john 5678 | peter </code></pre> <p>I would like to be able to generate query results like so... </p> <pre><code>results; other_person_id | other_person_nickname | last_message | last_message_timestamp 1234 | john | testing reply | 1290233089 </code></pre> <p>For the life of me, I cant figure out this query....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload