Mysql query for messaging system

I am developing a messaging system and cannot write a query (MYSQL). In a nutshell, the request should return an array of the most recent messages for each user in the session.

CREATE TABLE 'tbl_message'('id' int(10) AUTO_INCREMENT,
  'from_user' int(10),
  'to_user' int(10),
  'reply_to' int(10),
  'subject',
  'body' text,
  'status' tinyint(3),
  'deleted_from_sender' tinyint(3),
  'deleted_from_recipient' tinyint(3)',
  'created_on' datetime,

      

I wrote a request:

SELECT * FROM tbl_message s1 WHERE s1.created_on IN (
   SELECT MAX(s2.created_on)
      FROM tbl_message s2 WHERE s1.from_user=".$userID." OR s2.to_user=".$userID."
      GROUP BY s2.from_user, s2.to_user) 
       AND status = 0";

      

This works great, but give me the last 2 messages from from_user and to_user, instead of the last 1 message from both. This is because of GROUP BY, of course the question now is how can I find the max created_on (actually mysql datestamp) in the subquery? Or any other solution would be appreciated. Appreciate any help or advice.

Dig out stackoverflow and mysql instructions after 2 days, hoping for help from DB prof :)

UPD: some data, for example

+----+-------+--------+--------+---------------------+--------+---------+
| id | from_user | to_user | subject      | created_on          | status |
+----+-------+--------+--------+---------------------+--------+---------+
|  1 |   68      |   128   | somesubject1 | 2013-07-01 21:31:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  2 |    128    |   68    | somesubject2 | 2013-07-01 21:41:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  3 |    128    |   68    | somesubject3 | 2013-07-01 21:51:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  4 |    68     |   226   | somesubject4 | 2013-07-01 22:01:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+

      

query output

|  3 |    128    |   68    | somesubject3 | 2013-07-01 21:51:29 |      0 |
+----+-------+--------+--------+---------------------+--------+---------+
|  4 |    68     |   226   | somesubject4 | 2013-07-01 22:01:29 |      0 |

      

+3


source to share


1 answer


You can use this query to get the maximum create_on date between two users:

SELECT
  LEAST(from_user, to_user) user1,
  GREATEST(from_user, to_user) user2,
  MAX(created_on) max_created_on
FROM
  tbl_message
GROUP BY
  LEAST(from_user, to_user),
  GREATEST(from_user, to_user)

      

and the query you are looking for is probably this:



SELECT t.*
FROM
  tbl_message t INNER JOIN (
    SELECT
      LEAST(from_user, to_user) user1,
      GREATEST(from_user, to_user) user2,
      MAX(created_on) max_created_on
    FROM
      tbl_message
    WHERE from_user=68 or to_user=68
    GROUP BY
      LEAST(from_user, to_user),
      GREATEST(from_user, to_user)) M
  on t.created_on = M.max_created_on
     AND LEAST(t.from_user, t.to_user)=user1
     AND GREATEST(t.from_user, t.to_user)=user2

      

See the fiddle with two requests here .

0


source







All Articles