选出所有 用户数>10 的用户组:select * from `group` as g where 10<( select count(*) from `user` as u where u.groupid=g.groupid  );语句没报错,但得到的结果是不正确的,高手说下为什么,要怎么改.

解决方案 »

  1.   

    select g.*
    from `group` g inner join (select groupid from `user` group by groupid having count(*)<10) b
     on g.groupid=b.groupid
    == 思想重于技巧 ==
      

  2.   

    对了,group 表新增一列 num
    然后
    update group as g set num=(select count(*) from `user` as u where u.groupid=g.groupid );
    select * from group where num>10;这样选出来的结果是对的
      

  3.   

    当然不行啦,不报错不代表没错。你select出一个count(*)的列出来和10对比,但是group这个表根本没有count(*)的这列,没有这个列你怎么where都不会找到合符条件的记录的。
      

  4.   

    表 `group`:
    CREATE TABLE `group` (
    `groupid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `groupname` VARCHAR( 255 ) NOT NULL 
    ) ENGINE = MYISAM ;
    表 `user`:CREATE TABLE `user` (
    `userid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 255 ) NOT NULL ,
    `groupid` INT( 11 ) NOT NULL 
    ) ENGINE = MYISAM ;
    MySQL 版本: 5.1.23-rc-community