sql的分组排序

来源:互联网 发布:const javascript 编辑:程序博客网 时间:2024/04/30 17:40

group by 与 order by 的使用

之前使用的时候,网上查了下 都说group by 和order by 后面的参数必须是select 中的参数,后来自己试了下,只需要是from 表中的字段都行

 select
       id ,
       title,
       score,
       creator_name ,
       name,
       create_time,
       answer 
    from
        questions questions0_,
        question_type questionty1_
    where
        questions0_.question_typeid=questionty1_.id
        and questions0_.examlib_id=5700953400979733890
    group by
        questions0_.question_typeid


group by 分组,只是去取同类型的第一条数据,不会把所有的数据都分组,鉴于此,后来发现,如果要分组排序的话,只用order by即可:

 select
       id ,
       title,
       score,
       creator_name ,
       name,
       create_time,
       answer 
    from
        questions questions0_,
        question_type questionty1_
    where
        questions0_.question_typeid=questionty1_.id
        and questions0_.examlib_id=5700953400979733890
    order by
        questions0_.question_typeid,questions0_.create_time desc



0 0
原创粉丝点击