来自SQL的CakePHP子查询

时间:2011-12-09 13:35:42

标签: mysql cakephp cakephp-1.3 subquery

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

SELECT *
FROM (

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

使用子查询或编写SQL语句是否更好?

2 个答案:

答案 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分组。

答案 1 :(得分: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'
    )
);