2011-12-09 123 views
2
CREATE TABLE IF NOT EXISTS `messages` (
    `id` int(11) unsigned NOT NULL auto_increment, 
    `user_id` int(11) unsigned NOT NULL, 
    `node_id` int(11) unsigned NOT NULL, 
    `reciever_id` int(11) unsigned NOT NULL, 
    `created` datetime default NULL, 
    `modified` datetime default NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ; 

INSERT INTO `messages` (`id`, `user_id`, `node_id`, `reciever_id`, `created`, `modified`) VALUES 
(1, 1, 1, 15, '2011-12-07 00:00:00', '2011-12-07 02:00:00'), 
(2, 15, 1, 1, '2011-12-07 02:00:00', '2011-12-07 02:00:00'), 
(3, 15, 2, 1, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(4, 1, 2, 15, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(5, 1, 3, 18, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(6, 18, 3, 1, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(7, 1, 4, 18, '2011-12-07 12:00:00', '2011-12-07 12:00:00'), 
(8, 18, 4, 1, '2011-12-07 12:00:00', '2011-12-07 12:00:00'); 


CREATE TABLE IF NOT EXISTS `nodes` (
    `id` int(11) unsigned NOT NULL auto_increment, 
    `message` text NOT NULL, 
    `author_id` int(11) unsigned NOT NULL, 
    `read` tinyint(1) default NULL, 
    `created` datetime default NULL, 
    `modified` datetime default NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; 

INSERT INTO `nodes` (`id`, `message`, `author_id`, `read`, `created`, `modified`) VALUES 
(1, 'Hi! How are you ? dude wanna meet up this weekend ?', 1, 0, '2011-12-07 02:00:00', '2011-12-07 02:00:00'), 
(2, 'Sure. wanna go to Mangalore Pearl to eat Neer Dosa..', 15, 0, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(3, 'Hi How are u Buddy ? Long time no see...', 1, 0, '2011-12-07 11:00:00', '2011-12-07 11:00:00'), 
(4, 'yeah. are you back in town ? i think we should meet up man. its been ages ....', 18, 0, '2011-12-07 12:00:00', '2011-12-07 12:00:00'); 


CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) unsigned NOT NULL auto_increment, 
    `first_name` varchar(255) default NULL, 
    `last_name` varchar(255) default NULL, 
    `email` varchar(255) default NULL, 
    `password` varchar(40) default NULL, 
    `username` varchar(255) default NULL, 
    `birthday` date default NULL, 
    `gender` varchar(255) default NULL, 
    `city_id` int(11) unsigned NOT NULL, 
    `status` varchar(255) NOT NULL, 
    `created` datetime default NULL, 
    `modified` datetime default NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ; 

我想在Cake中创建一个子查询。但不知道如何下手:(来自SQL的CakePHP子查询

这是我想要执行

SELECT * 
FROM (

SELECT * 
FROM messages AS msg 
WHERE user_id =1 
ORDER BY modified DESC 
) AS latest_message 
GROUP BY reciever_id 

是它更好地使用子查询或写SQL语句的SQL?

+1

如果你可以列出你的表的细节和你想回来的几行数据,它会帮助人们给出很好的答案。 – Dave

回答

0

由于您试图按接收方进行分组,因此为什么不更改查询以检索接收方,然后查看属于每个接收方的消息?下面,我假设使用可容忍的行为。

$this->Receiver->find('all', array(
    'contain' => array(
     'Message' => array(
      'conditions' => array('Message.user_id' => 1), 
      'order' => array('Message.modified' => 'DESC'), 
     ) 
    ) 
)); 

编辑

我加入这个查询,看看它是否有助于根据您的评论。

$this->Message->find(
    'all', 
    array(
     'conditions' => array('Message.user_id' => 1), 
     'fields' => array('Message.*', 'MAX(Message.modified) as max_mod'), 
     'group' => 'Message.receiver_id' 
    ) 
); 
+0

我想从/与我交谈的每个用户的最新消息 –

+0

看看新的编辑给你你想要的。祝你好运! –

+0

我收到两条记录。但这些记录是线程的第一条消息。我想要修改的最后一条消息。 –

1

我真的不知道是什么的必要性,子查询就在这里,就不会像这样做的伎俩?

$this->Message->find('all', array(
    'conditions' => array('Message.user_id' => 1), 
    'order' => array('Message.modified' => 'DESC'), 
    'group' => array('Message.receiver_id') 
)); 

这将检索所有来自用户的信息id为1,按日期排序,修改和分组通过receiver_id。

+0

我试过了。先将它分组,然后进行订购。它不会获取预期的结果。 –