使用相同的父ID将行组合在一起

时间:2011-05-11 04:27:04

标签: php mysql

我正在编写一个私人消息脚本。但是,我在MySQL端遇到了一些问题。我已经部分工作了。

一个例子:

messageid | parentid | subject | flags
1           NULL       'Foobar'  2     //has been read
2           1          'Foobar'  2     //has been read
3           1          'Foobar'  1     //has not been read

messageid:自动递增邮件
parentid:主题中第一条消息的messageid 主题的主题主题(或标题)
标志:逐位标志(1 =未读; 2 =读取)

问题(希望有人可以帮我解决)
1.我现在设置它的方式,我的脚本将在未读消息页面和阅读消息页面中显示消息线程。我的目标是让它只在未读消息页面中显示,直到它被读取。

这是我的查询

//$_GET['node'] is allowed to be: unread, read, or sent  
$wftype = $_GET['node'] == 'sent' ? 'sender' : 'recipient';  
$filter = $_GET['node'] == 'sent' ? '' : ' AND (`flags` & '.$message_flags[$_GET['node']].') != 0';  
//$filter = ($_GET['node'] == 'unread' || $_GET['node'] == 'read') ? ($_GET['node'] == 'read' ? ' AND (`flags` & '.$message_flags['read'].') != 0' : ' AND (`flags` & '.$message_flags['unread'].') != 0') : '';  
$result = $sql->query('SELECT `messageid`, `parent`, `senderid`, `sender`, `subject`, MAX(`sendtime`) AS `sendtime` FROM `memberpostbox` WHERE `'.$wftype.'id` = '.$_SESSION['client']['number'].$filter.' AND (`flags` & '.$message_flags[$wftype.'_deleted'].') = 0 GROUP BY `parent` ORDER BY `sendtime` DESC');  

我是否需要重组我的桌子或彻底改变我的表现?或者,这可以用我现在拥有的东西来完成吗?

补充资料
表结构

CREATE TABLE IF NOT EXISTS `memberpostbox` (  
  `messageid` bigint(20) unsigned NOT NULL auto_increment,  
  `parentid` int(10) unsigned default NULL,  
  `senderid` varchar(255) collate utf8_unicode_ci NOT NULL,  
  `sender` varchar(255) collate utf8_unicode_ci NOT NULL,  
  `recipientid` varchar(255) collate utf8_unicode_ci NOT NULL,  
  `recipient` varchar(255) collate utf8_unicode_ci NOT NULL,  
  `subject` varchar(255) collate utf8_unicode_ci default NULL,  
  `message` longtext collate utf8_unicode_ci NOT NULL,  
  `sendtime` int(10) unsigned NOT NULL,  
  `flags` tinyint(3) unsigned NOT NULL default '0',  
  PRIMARY KEY  (`messageid`),  
  KEY `groupid` (`parentid`),  
  FULLTEXT KEY `search` (`subject`,`message`)  
ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=22 ;  

1 个答案:

答案 0 :(得分:0)

对于未读邮件

'SELECT .... FROM memberpostbox WHERE flag=1'

用于阅读消息

'SELECT .... FROM memberpostbox WHERE flag=2'

只有标志确定是否读取了消息,因此这将是显示正确列表的唯一部分.....当然,您必须在其中添加其余代码。< / p>

// read
$result = $sql->query(
    'SELECT 
       `messageid`, 
       `parent`, 
       `senderid`,
       `sender`, 
       `subject`, 
       MAX(`sendtime`) AS `sendtime` 
    FROM `memberpostbox` 
    WHERE `recipientid` = '.$_SESSION['client']['number'].$filter.' 
    AND `flags` = 2
    AND '.$message_flags[$wftype.'_deleted'].' = 0
    GROUP BY `parent` 
    ORDER BY `sendtime` DESC'
  );