有个表table_a,里面有三个字段和一些值,如下
id      num      b_id
1       1        1
2       2        1
3       3        1
4       4        1
5       1        2
6       2        2
7       3        2我现在想根据b_id group,然后取出不同的b_id中的最大的num和这个最大num对应的id
比如说上面这个例子,取出来的结果就是
id     max(num)     b_id
3      4            1
5      3            3

解决方案 »

  1.   

    对不起,打错了,取出来的结果应该是
    id     max(num)     b_id
    4      4            1
    7      3            2
      

  2.   

    select id,max(num),bid from table_a group by id,b_id
      

  3.   

    select one.id,num,b_id from(select id,max(num) as num from table_a group by id) as oneleft join(select id,b_id from table_a) as a1 on a1.id=one.id这个应该更准确
      

  4.   

    askyin() 不行啊,这样取出来的还是很多条,不是两条啊好像
      

  5.   

    你要取出两条的条件是设么?为设么出现两条?  “我现在想根据b_id group,然后取出不同的b_id中的最大的num和这个最大num对应的id“取出不同的b_id中的最大的num和这个最大num对应的id应该是 
    1       1        1
    2       2        1
    3       3        1
    4       4        1
    5       1        2
    6       2        2
    7       3        2都出来   你的
    id     max(num)     b_id
    4      4            1
    7      3            2
    结果  规则是设么?
      

  6.   

    select id,num,bid from table_a desc num group by b_id试试
      

  7.   

    select id,max(num) aa,b_id from table_a group by b_id desc;
    再试试
      

  8.   

    /* try like this */select * from table_a a
    where num=(select max(num) from table_a
               where b_id=a.b_id);
      

  9.   

    mschen(Co-ok) 谢谢你,就是这个,多谢了