开发者

Problem with SQL query. Need to distinct my results

I got table with my conversations, and i need to list alle conversation as topics. So if I'm the sender or the receiver, it should list it as one conversation. So between 2 users, it doesn't matter which role they got.


So if I got:

1, me, mom, "hello mom, I'm good. How are you?", 1, 23/09-2011
2, mom, me, "hello son, how are you?", 1, 22/09-2011
3, me, dad, "hello dad, how are you?", 1, 20/09-2011

I want to show it like this:

between You and Mom - Hello mom... - 23/09-2011 - 开发者_开发百科2 messeges
between You and Dad - Hello dad... - 20/09-2011 - 1 message

I can't seem to figure out the query. Something with DISTINCT maybe.

I hope you guys can help me out :)

Update

CREATE TABLE IF NOT EXISTS `fisk_beskeder` (
  `id` int(11) NOT NULL auto_increment,
  `fra` int(11) NOT NULL,
  `til` int(11) NOT NULL,
  `dato` varchar(50) NOT NULL,
  `set` int(11) NOT NULL default '0',
  `besked` text NOT NULL,
  `svar` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

PHP

$sql = mysql_query(
    "SELECT `fra`, `til`, `besked`, `dato`, `set`, `id` 
    FROM fisk_beskeder 
    WHERE til = ".$userid." 
       OR fra = ".$userid." 
    ORDER BY id DESC") 
or die(mysql_error());

Update

Okay, it's working now. Only thing i need is to get the newest data from the group.

SELECT count(id), `id`, `fra`, if(a.fra = $cfg_brugerid, a.til, a.fra) AS other, `til`, `besked`, `dato`, `set` 
FROM fisk_beskeder a
WHERE fra = $cfg_brugerid OR til = $cfg_brugerid
GROUP BY other
ORDER BY a.id DESC


Maybe this:

SELECT COUNT(msg), msg, a.date,  if(a.from ='me', a.to , a.from) as otherPerson
FROM table a  
WHERE a.from = 'me' or a.to = 'me'
GROUP BY otherPerson


Something like.

SELECT COUNT(a.msg), a.msg, a.date 
FROM table a, table b 
WHERE a.from = b.from
AND a.to != b.to
GROUP BY a.from
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜