求高效率SQL

求高效SQL

基本信息表

CREATE TABLE `message` (

  `id` int(1) NOT NULL auto_increment,

  `title` varchar(128) default NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

基本信息的评论表

CREATE TABLE `comment` (

  `cid` int(1) NOT NULL auto_increment,

  `id` int(1) default NULL,

  `msg` varchar(123) default NULL,

  PRIMARY KEY (`cid`)

) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

现在要求查询message表里面。对应评论总数最多的2条记录。

我目前用group by这么实现了。同学说,如果不用group by 也有办法实现,但是我想不到。所以请求大家,有没有最高效的针对这个SQL的查询语句

select m.id,m.title,count(c.id) as cc from message m, comment c where m.id =c.id group by title order by cc desc limit 2;



select b.*

from (select id,count(*) from comment group by id order by 2 desc limit 2) a ,message b 

where a.id=b.id

求高效率SQL

相关文章:

你感兴趣的文章:

标签云: