基本信息表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;

解决方案 »

  1.   

    能写条代码吗。呵呵。SQL如何写呢
      

  2.   

    就用这个sql搞成定时任务   五分钟执行一次 insert into temp(id,title,cc)
    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;
    然后让程序实时的读temp表
      

  3.   

    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
      

  4.   

    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
      

  5.   

    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;感觉这条SQL语句效率不高。还有比这个更高效的写法吗。
      

  6.   

    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;感觉这条SQL语句效率不高。还有比这个更高效的写法吗。
      

  7.   

    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
      

  8.   

    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