-1

我在mysql中有一张表

comment_id   comment_by_user   comment_date                comment_body
  1244         david_sam       2013-02-27 15:46:53          xyz
  1245         raymond_davis   2013-02-27 13:46:53          xyz
  1246         jam_jam         2013-02-14 18:46:53          xyz

我想将此数据显示为

2013-02-27 -------------------------------------
1244       david_sam        15:46      xyz     
1245       raymond_davis    13:46      xyz
2013-02-14 -------------------------------------
1246       jam_jam          18:46      xyz

我该怎么做,哪个 SQL 查询和哪个 PHP 方法?

4

2 回答 2

2

您只需选择所有需要的列

select comment_id, comment_by_user,
       date(comment_date), time(comment_date), comment_body
from comments
order by comment_date desc, comment_id

然后在日期更改时输出行并插入附加行

$current_date = '';
while (list($id, $user, $date, $time, $body) = $sth->fetch(PDO::FETCH_NUM)) {
    if ($date != $current_date) {
        echo "$date ----\n";
        $current_date = $date;
    }

    echo "$id $user $time $body\n";
}
于 2013-03-11T11:00:09.007 回答
0

试试这个。。

SELECT * FROM `table_name` ORDER BY `comment_date` DESC

你的php代码应该是这样的..

$date = '';
foreach($records as $record)
{
    if($date != date('Y-m-d'), strtotime($record->comment_date))
    {
        //  print "2013-02-14 -------------------------------------";
        $date = date('Y-m-d'), strtotime($record->comment_date);
    }

    //  print "1246       jam_jam          18:46      xyz";
}
于 2013-03-11T11:01:01.833 回答