现在有张表test
id    test
1     0
2     1
3     0
4     1
5     0现在是要首先对test字段进行分组,然后在根据id进行排列,小弟对sql不是很懂,希望大家解答一下,谢谢

解决方案 »

  1.   

    分组一般是查询前面用上了SUM,COUNT,AVG等函数,用GROUP BY--排序的例子
    --这个是按照ID,从小到大排序
    SELECT * FROM test  ORDER BY ID--这个是按照ID,从大到小排序
    SELECT * FROM test  ORDER BY ID DESC
     
      

  2.   

    ls说得对
    group by一般是要跟聚合函数使用的
    你的需求这样应该可以了:select * from test order by test desc, id desc;
    --或
    select * from test order by test desc, id;
    --随便乱调
      

  3.   

    楼主如果对id进行排序就不需要分组了,直接用
    select * from test order by test desc,id desc;
    如果要分组可以用下面的语句:
    SELECT MAX(ID),TEST FROM TEST GROUP BY TEST ORDER BY MAX(ID);
      

  4.   

    create table test
    (
    id int,
    test int
    )insert into test values(1,0)
    insert into test values(2,1)
    insert into test values(3,0)
    insert into test values(4,1)
    insert into test values(5,0)--分组排序
    select id,  test from test
    group by test,id
    order by test ,id 
    select id,  test from test
    group by test,id
    order by test ,id desc
      

  5.   

    不需分组,select * from test order by test, id 排序即可