Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MY SQL Query for thread messaging inbox and sent

I'm trying to create the thread messaging system in the PHP and mySQL. My messaging table is as follow

SELECT `es_id`, `es_fid`, `es_tid`, `es_subject`, `es_message`, `es_onstamp`, `es_msg_read`, `es_f_del`, `es_t_del`, `threadid` FROM `esb2b_messages`

In this table

es_id = primary key for the table.
es_fid = This field store the from user id.
es_tid = This field store the to user id.
es_subject = This field store the subject of message.
es_message = This field store the body of message.
es_onstamp = This field store the time stamp.
es_msg_read = This field store if receiver open the message.
es_f_del = This field store if the from user delete this message.
es_t_del = This field store the the to user delete this message.
threadid = This field store the id of parent message(es_id of the replied message).

Please refer this image for clear understanding what i want to archive.

http://oi59.tinypic.com/2wdav45.jpg

In the picture above the combination of these 4 messages create one thread.

What query should be for the inbox and for sent item. Right now I'm using this query for inbox but every message is separate.

SELECT `es_id`, `es_fid`, `es_tid`, `es_subject`, `es_message`, `es_onstamp`, `es_msg_read`, `es_f_del`, `es_t_del`, `threadid` FROM `esb2b_messages` WHERE `es_tid`= UNHEX('$Loginuser') ORDER BY `esb2b_messages`.`es_onstamp` DESC

Please also see this image to see what is output right now and what i want.

Please note: inbox should be sorted and display only by last message received in thread. Same sent item should be sorted and displayed only by last message sent in thread.

SQL

CREATE TABLE IF NOT EXISTS `esb2b_messages` (
  `es_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `es_fid` bigint(20) NOT NULL DEFAULT '0',
  `es_tid` bigint(20) NOT NULL DEFAULT '0',
  `es_subject` mediumtext NOT NULL,
  `es_message` longtext NOT NULL,
  `es_tempdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `es_onstamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `es_msg_read` varchar(10) NOT NULL DEFAULT '',
  `es_f_del` varchar(10) NOT NULL DEFAULT '',
  `es_t_del` varchar(10) NOT NULL DEFAULT '',
  `threadid` varchar(255) NOT NULL DEFAULT '0',
  PRIMARY KEY (`es_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=367 ;

--
-- Dumping data for table `esb2b_messages`
--

INSERT INTO `esb2b_messages` (`es_id`, `es_fid`, `es_tid`, `es_subject`, `es_message`, `es_tempdate`, `es_onstamp`, `es_msg_read`, `es_f_del`, `es_t_del`, `threadid`) VALUES
(361, 3, 23, ' Quotation for Embossed and watermark Certificate Printing', 'Hello, this is Bella, we specilized in providing variable data printing services for eight years and we can make it according to your detail requests. Could you please send your original artpaper sothat we can discuss it in details? Looking forward to hearing from you soon. My skype is kingwin1688.', '2014-08-23 22:40:39', '2014-08-23 21:59:55', 'Yes', 'No', 'No', '0'),
(360, 2, 23, 'test', 'asgdfgdfsgdf', '2014-08-23 19:39:11', '2014-08-19 02:35:09', 'Yes', 'No', 'No', '0'),
(363, 2, 23, 'not threaded', 'asgdfgdfsgdf', '2014-08-23 23:29:28', '2014-08-19 02:35:09', 'Yes', 'No', 'No', '0'),
(362, 23, 2, ' Quotation for Embossed and watermark Certificate Printing', 'Hello, this is Bella, we specilized in providing variable data printing services for eight years and we can make it according to your detail requests. Could you please send your original artpaper sothat we can discuss it in details? Looking forward to hearing from you soon. My skype is kingwin1688.', '2014-08-23 19:39:15', '2014-08-23 21:59:55', 'No', 'No', 'No', '0'),
(364, 23, 2, 'reply', 'my first reply', '2014-08-23 21:41:11', '2014-08-23 02:35:09', 'No', 'No', 'No', '360'),
(365, 2, 23, 'this is reply of reply', 'reply of reply', '2014-08-23 22:41:26', '2014-08-24 02:35:09', 'Yes', 'No', 'No', '360'),
(366, 23, 2, 'reply', 'my first reply', '2014-08-23 21:41:11', '2014-08-24 02:35:09', 'No', 'No', 'No', '360');

Link to sql fiddle http://www.sqlfiddle.com/#!2/9def4/1/0

The es_fid and es_tid are foreign key for the user table.

The output for inbox should be

From | Subject | Message | TimeStamp

In group

Let me explain how this system work, if the 'threadid' is 0 this mean the row is parent. if the 'threadid' in row is not zero it means it is child to one parent. i want whenever user goes to inbox it only display latest received in child if exits else display parent.

Thanks.

like image 724
Faiz Rasool Avatar asked Oct 01 '22 00:10

Faiz Rasool


1 Answers

Fiddle: http://www.sqlfiddle.com/#!2/9def4/13/0

select es_id, es_fid, es_subject, es_message, es_onstamp
  from esb2b_messages x
 where threadid = 0
   and es_tid = 23
   and not exists (select 1
          from esb2b_messages y
         where y.threadid = x.es_id
           and y.es_tid = x.es_tid)
union all
select es_id, es_fid, es_subject, es_message, es_onstamp
  from esb2b_messages x
 where threadid <> 0
   and es_tid = 23
   and es_onstamp = (select max(y.es_onstamp)
                       from esb2b_messages y
                      where y.threadid = x.threadid)
 order by es_id, seq, es_onstamp

360 is now reduced to the latest child as discussed in comments.

The other threads have no children, so the parent is shown. If they did have children, the latest child would be shown.

like image 122
Brian DeMilia Avatar answered Oct 03 '22 02:10

Brian DeMilia