开发者

Grouping rows together with the same parent-ID

I am writing a private-messaging script. However, I have run into a few problems on the MySQL end. I've got it partially working.

An example:

messageid | parentid | subject | flags
1           NULL       'Foobar'  2     //has开发者_JS百科 been read
2           1          'Foobar'  2     //has been read
3           1          'Foobar'  1     //has not been read

messageid: auto-incremented of the message

parentid: messageid of the first-message in the thread

subject: subject (or title) of the thread

flags: bit-wise flags (1 = unread; 2 = read)

The problems (which hopefully someone can help me solve)

1. The way I have it setup right now, my script will display the message-thread in the unread-messages page and the read-messages page. My goal is to get it to display in only the unread-messages page until its been read.

Here is my Query

//$_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');  

Will I have to restructure my table or completely change how I'm doing this? Or, can this be accomplished with what I have now?

Additional information

Table-structure

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 ;  


For Unread messages

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

for read message

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

only the flag is determining if the message is read or not, so that will be the only part involve to display the correct list..... of course you will have to add the rest of your code in there.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜