我在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方法?
答案 0 :(得分: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";
}
答案 1 :(得分: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";
}