如何按日期获得最近10位用户

时间:2016-09-07 04:53:46

标签: sql oracle greatest-n-per-group

我有一个名为users的表,其中包含username和created_date列。我希望通过使用查询获得10个最近创建的用户。

查询:

select to_char(created_date, 'DD-MM-YYYY')
  from users
 order by created_date desc;

这将返回按降序创建的用户。但我希望得到前10条记录。

4 个答案:

答案 0 :(得分:1)

尝试使用子查询和ROWNUM SELECT * FROM (SELECT * FROM users ORDER BY created_date DESC ) WHERE ROWNUM <= 10

答案 1 :(得分:0)

附加到您的查询:

$(document).ready(function(){ 
$('#from_date').datepicker( {
        changeMonth: true,
        changeYear: true, 
        dateFormat: "yy-mm-dd",
         maxDate: new Date(),

        onSelect: function (selectedDate) {
                var orginalDate = new Date(selectedDate);
                var monthsAddedDate = new Date(new Date(orginalDate).setMonth(orginalDate.getMonth() + 1));

                $("#to_date").datepicker("option", 'minDate', selectedDate);
                $("#to_date").datepicker("option", 'maxDate', monthsAddedDate);
            }
    }).click(function(){
        $('.ui-datepicker-calendar').show();
    });
    $('#to_date').datepicker( {
        changeMonth: true,
        changeYear: true, 
        dateFormat: "yy-mm-dd",
         maxDate: new Date(),

        onSelect: function (selectedDate) {
                var orginalDate = new Date(selectedDate);
                var monthsAddedDate = new Date(new Date(orginalDate).setMonth(orginalDate.getMonth() - 1));

                $("#from_date").datepicker("option", 'minDate', monthsAddedDate);
                $("#from_date").datepicker("option", 'maxDate', selectedDate);
            }
    }).click(function(){
        $('.ui-datepicker-calendar').show();
    });
});
</script>

因此您只获得前10个结果。

答案 2 :(得分:0)

嗨Srinivas
select username,to_char(created_date, 'DD-MM-YYYY') from user where (username,created_date) in (select username,created_date from user order by created_date desc) and rownum <=10

希望它会有用。

答案 3 :(得分:-1)

$result = mysqli_query($con,"SELECT username FROM users ORDER BY created_date DESC LIMIT 0, 10");