按日期列出数据库记录

时间:2015-08-19 21:13:59

标签: sql vb.net

我需要在ul列表中按日期排序表。

想象一下下表:

ID      Date        Name
1       02-05-2015  Example
2       02-05-2015  Example2
3       04-08-2015  Example3

所以我想列出这样的sql表:

02-05-2015
Example
Example2
04-08-2015
Example3

我怎样才能实现这一目标?值得一提的是,我在ASP.net(vb)中这样做。

由于

3 个答案:

答案 0 :(得分:0)

您可以执行按日期排序的查询,然后使用脚本语言迭代结果(我在php中显示)

$count = 0;
while($queryResults = mysqli_fetch_assoc($query)) {
    count += 1;
    if($count == 1) {
        // IF FIRST ITERATION, ASSIGN ROW'S DATE TO $prevDate
        $prevDate = $queryResults['date'];
    } else {
        // IF NOT FIRST ITERATION, CHECK IF THIS ROW'S DATE IS EQUAL TO $prevDate
        if($queryResults['date'] == $prevDate) {
            //IF THEY MATCH, SHOW THE NAME FROM THIS ROW
            echo $queryResults['name'];
        } else {
            //IF THEY DON'T MATCH, SHOW THIS ROW'S DATE
            echo $queryResults['date'];
            //SHOW NAME FROM THIS ROW OR YOU WILL BE MISSING A LOT OF ROWS IN THE RESULTING DISPLAY
            echo $queryResults['name'];
            //NOW ASSIGN THIS ROW'S DATE TO $prevDate FOR THE NEXT DATE TEST
            $prevDate = $queryResults['date'];
        }
    }
}

答案 1 :(得分:0)

执行匿名阻止或为此创建一个函数,如下所示:

create table mytable (id number, date_col date, name varchar2(32));

declare
v_date date;
v_number number;
begin
  for date_rec in (select date_col from mytable group by date_col) loop
    dbms_output.put_line(date_rec.date_col);

    for rec in (select * from mytable where date_col = date_rec.date_col ) loop
        dbms_output.put_line(rec.name || ' ' || rec.name);      
    end loop; 
  end loop;
end;

答案 2 :(得分:0)

如果您想按最近的日期订购结果,那么您只需添加order by date desc