【问题标题】:MY SQL Query for thread messaging inbox and sentMY SQL 查询线程消息收件箱并发送
【发布时间】:2014-08-24 01:06:07
【问题描述】:

我正在尝试在 PHP 和 mySQL 中创建线程消息传递系统。我的消息表如下

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`

在这张表中

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).

请参考此图片以清楚了解我要存档的内容。

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

在上图中,这4条消息的组合创建了一个线程。

应针对收件箱和已发送邮件进行什么查询。现在我正在使用这个查询收件箱,但每封邮件都是分开的。

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

也请看这张图片,看看现在输出什么以及我想要什么。

请注意:收件箱应仅按线程中收到的最后一条消息进行排序和显示。相同的已发送项目应仅按线程中发送的最后一条消息进行排序和显示。

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');

链接到 sql fiddle http://www.sqlfiddle.com/#!2/9def4/1/0

es_fid 和 es_tid 是用户表的外键。

收件箱的输出应该是

来自 |主题 |留言 |时间戳

在群里

让我解释一下这个系统是如何工作的,如果 'threadid' 为 0,这意味着该行是父行。如果行中的“threadid”不为零,则表示它是一个父级的子级。我希望每当用户进入收件箱时,它只显示最新收到的子项,如果退出则显示父项。

谢谢。

【问题讨论】:

  • 你能显示你期望的输出吗?
  • 应该和Gmail一样,es_id和threadid相同的邮件应该显示一次。在收件箱中,应显示最近收到的消息,在已发送的消息中,应将最近发送的消息显示为群组中所有其他消息的线程。
  • 只是用红色括起来的 2 行还是全部 4 行?此外,预期的结果列是“From”、“Subject”、“Message”、“Received”,对吗?如果是这样,“发件人”列来自哪里?它不在您发布的桌子上。
  • 您能否发布该表的 CREATE 语句和一些示例数据的 INSERT,以便我们可以定位您所追求的结果(在 phpmyadmin 中点击导出)

标签: php mysql sql database-design


【解决方案1】:

小提琴: 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 现已缩减为 cmets 中讨论的最新子代。

其他线程没有子线程,所以显示父线程。如果他们确实有孩子,则会显示最新的孩子。

【讨论】:

  • 我需要 2 个查询,一个用于收件箱,一个用于已发送项目,假设收件箱 (sqlfiddle.com/#!2/9def4/3) 使用此条件获取收件箱 es_tid='23',对于已发送使用此条件获取发件箱es_fid='23'。在我发布的小提琴中,您将看到 ES_ID 360 和 365 是一个线程的一部分,因为 ED_ID 365 包含 360 的线程 ID。我想显示线程的唯一最新版本。
  • 查看我的编辑。如果您发布您期望的输出,这将有很大帮助。 “最新版本的线程”是什么意思?
  • 请看这张图片。 s28.postimg.org/k96ymr6yz/new_clear.jpg。我想要它作为一排。那只是上面的那个。 es_id 365 一个
  • 好的,让我解释一下这个系统是如何工作的,如果'threadid'为0,这意味着该行是父行。如果行中的“threadid”不为零,则表示它是一个父级的子级。我希望每当用户进入收件箱时,它只显示最新收到的子项,如果退出则显示父项。
  • 我得到了父/子的东西,上面的查询实现了。但是,如果它只带回每个父母的最新孩子(有孩子的地方),那不是图像中两行中的第二行吗?第一行是父母,第二行是唯一的孩子。这将使唯一的孩子成为最新的孩子。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多