Mysql从每个组中获得最新的10个

时间:2013-09-23 04:09:42

标签: php mysql

我在zend框架方式中询问了如何做到的前一个问题,但没有运气,帮我获得常规的mysql查询。

我的表格messages包含message_idthread_idmessage

我有一个清单

$threads = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";

我需要从每个帖子中找到最后10条消息。

我正在尝试按照

的方式行事
SELECT m2.* FROM messages m2 JOIN(SELECT * FROM messages)m ON m2.thread_id = m.thread_id WHERE m2.message_id IN ('$list') LIMIT 10

但总共限制了10个。

任何帮助将不胜感激,谢谢。


 CREATE TABLE IF NOT EXISTS `messages` (
  `message_id` int(11) NOT NULL AUTO_INCREMENT,
  `thread_id` int(11) NOT NULL,
  `message` text NOT NULL,
  PRIMARY KEY (`message_id`),
  KEY `thread_id` (`thread_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

示例数据:

    INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1,1,"tempor erat neque");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (101,1,"adipiscing non, luctus sit amet,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (201,1,"fames");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (301,1,"odio. Phasellus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (401,4,"Donec egestas. Aliquam nec enim. Nunc ut");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (501,3,"Aliquam tincidunt, nunc ac mattis ornare, lectus ante dictum mi,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (601,5,"Fusce mollis. Duis sit amet diam eu dolor egestas rhoncus.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (701,2,"non, lacinia at, iaculis quis, pede. Praesent");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (801,6,"vehicula");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (901,2,"mollis nec, cursus a, enim. Suspendisse");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1001,3,"urna. Nullam lobortis quam a felis ullamcorper");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1101,2,"magna. Lorem ipsum dolor sit amet, consectetuer");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1201,5,"erat nonummy ultricies ornare, elit elit fermentum risus,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1301,4,"mattis ornare, lectus ante dictum mi, ac mattis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1401,5,"justo. Proin non massa");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1501,6,"leo, in lobortis tellus justo sit amet nulla.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1601,5,"sit amet, dapibus id, blandit");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1701,1,"cursus luctus, ipsum leo elementum sem, vitae aliquam");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1801,4,"ac ipsum. Phasellus vitae mauris sit amet");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (1901,1,"nunc interdum feugiat. Sed nec metus facilisis lorem tristique aliquet.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2001,2,"varius. Nam porttitor scelerisque neque.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2101,4,"sollicitudin a, malesuada id, erat. Etiam vestibulum");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2201,3,"Sed congue, elit sed");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2301,2,"sagittis felis. Donec tempor, est ac mattis semper, dui lectus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2401,1,"ut aliquam iaculis, lacus pede sagittis augue,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2501,5,"at pretium aliquet, metus urna convallis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2601,3,"amet risus. Donec egestas.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2701,6,"pede. Cras vulputate velit");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2801,1,"mattis semper, dui lectus rutrum urna, nec luctus felis purus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (2901,2,"nec");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3001,1,"amet metus. Aliquam erat");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3101,3,"justo.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3201,3,"nec ante. Maecenas mi felis,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3301,1,"mi tempor lorem, eget mollis lectus pede");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3401,4,"non, hendrerit id, ante. Nunc mauris sapien, cursus in,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3501,3,"ullamcorper magna. Sed eu eros. Nam consequat");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3601,1,"in");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3701,2,"Nam ac nulla. In tincidunt congue turpis. In condimentum.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3801,1,"viverra. Donec tempus, lorem fringilla ornare placerat, orci lacus vestibulum");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (3901,4,"eget ipsum.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4001,6,"tempor diam");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4101,6,"turpis non enim. Mauris quis turpis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4201,3,"ac facilisis facilisis, magna tellus faucibus leo, in lobortis tellus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4301,3,"pharetra nibh.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4401,2,"nibh. Donec est mauris, rhoncus id, mollis nec,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4501,5,"id nunc interdum");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4601,6,"lorem tristique aliquet. Phasellus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4701,3,"orci lacus vestibulum lorem, sit amet");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4801,1,"nulla. Donec non justo. Proin non");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (4901,5,"consectetuer");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5001,3,"sed,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5101,4,"Quisque fringilla euismod enim. Etiam gravida molestie arcu. Sed eu");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5201,5,"dolor. Fusce mi lorem,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5301,3,"venenatis vel, faucibus id, libero. Donec consectetuer");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5401,4,"vitae semper egestas, urna justo faucibus lectus, a sollicitudin");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5501,5,"blandit viverra. Donec tempus, lorem fringilla ornare");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5601,4,"tincidunt pede ac urna. Ut tincidunt vehicula");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5701,2,"eu enim. Etiam imperdiet dictum magna. Ut tincidunt orci quis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5801,6,"augue");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (5901,4,"sapien imperdiet ornare. In faucibus. Morbi vehicula. Pellentesque");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6001,3,"lacus. Aliquam rutrum");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6101,3,"non sapien molestie orci tincidunt adipiscing. Mauris molestie pharetra");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6201,6,"at pretium aliquet, metus urna convallis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6301,3,"tincidunt adipiscing. Mauris molestie pharetra nibh. Aliquam ornare, libero at");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6401,1,"Phasellus at augue id ante dictum cursus.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6501,3,"et, eros. Proin ultrices.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6601,1,"molestie tortor nibh sit amet orci. Ut sagittis lobortis mauris.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6701,1,"laoreet lectus quis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6801,5,"dictum eleifend, nunc");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (6901,6,"sagittis. Duis gravida. Praesent eu nulla at sem molestie");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7001,1,"quis, tristique ac,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7101,3,"ut, sem. Nulla interdum. Curabitur dictum. Phasellus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7201,5,"metus facilisis lorem tristique aliquet. Phasellus");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7301,3,"dignissim pharetra. Nam ac nulla. In tincidunt congue turpis.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7401,5,"pede. Cum sociis natoque penatibus et magnis dis parturient");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7501,6,"Vestibulum ante ipsum primis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7601,5,"nascetur ridiculus mus. Donec");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7701,2,"a, arcu. Sed et libero.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7801,4,"posuere at, velit.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (7901,1,"lorem");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8001,2,"lacus. Nulla tincidunt,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8101,4,"massa rutrum magna. Cras convallis convallis dolor. Quisque");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8201,5,"mus. Proin vel nisl. Quisque fringilla");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8301,4,"justo faucibus lectus, a sollicitudin orci sem eget massa. Suspendisse");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8401,2,"amet massa. Quisque porttitor eros nec");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8501,4,"massa non ante bibendum ullamcorper.");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8601,2,"Curabitur sed");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8701,6,"lorem, vehicula et, rutrum");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8801,6,"euismod");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (8901,2,"amet,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9001,3,"arcu imperdiet ullamcorper. Duis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9101,5,"Donec vitae erat vel pede blandit congue. In scelerisque scelerisque");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9201,5,"Cras lorem lorem, luctus ut, pellentesque eget,");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9301,2,"gravida sagittis. Duis");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9401,5,"erat eget ipsum. Suspendisse sagittis. Nullam");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9501,6,"Mauris");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9601,4,"a sollicitudin orci sem eget");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9701,4,"nonummy ultricies ornare, elit elit");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9801,1,"pede sagittis augue, eu");
INSERT INTO `messages` (`message_id`,`thread_id`,`message`) VALUES (9901,5,"posuere vulputate, lacus. Cras interdum. Nunc");

2 个答案:

答案 0 :(得分:1)

您可以使用sql union并附加所有单个查询及其限制

$sql = "";
$threadsArray = explode($threads, ',');
foreach($threadArray as $key=>$value){
    $sql .= "(SELECT * FROM messages WHERE thread_id = {$value} LIMIT 10)";

    if ($key != (count($threadsArray) - 1)) $sql .= " UNION "; // Append union expect for last element
}

// Execute your query and filter the results to group messages from each thread

答案 1 :(得分:0)

如果这不是mysql,显而易见的解决方案是使用SQL聚合窗口函数之一。鉴于mysql约束,“explodybits”博客上的this article似乎显示了一个有效的解决办法,让mysql模仿它们。文章的总和似乎大致是下面的代码。

select * from (
    select @row_num := IF(@prev_value=m.thread_id,@row_num+1,1) AS RowNumber, 
           m.*,
           @prev_value := m.thread_id
    from messages m,
         (SELECT @row_num := 1) x,
         (SELECT @prev_value := 0) y
    where thread_id in (1,2,3)
    order by thread_id
) t1 where RowNumber <= 10;