我有一个mysql表:
+----+----------------------------+
| id | name | datetime |
+----+----------------------------+
| 1 | Qwe | 2012-11-20 08:00:00 |
| 2 | Foo | 2012-11-20 05:00:00 |
| 3 | Bar | 2013-01-01 08:00:00 |
+----+----------------------------+
如何使用mysql从db获取它:
date
id - time - name
2012-11-20
2 - Foo - 05:00
1 - Qwe - 08:00
2013-01-01
3 - Bar - 08:00
答案 0 :(得分:3)
一种方法
$sql = "SELECT id, name,
DATE_FORMAT(`datetimecol`, '%Y-%m-%d') mydate,
DATE_FORMAT(`datetimecol`, '%H:%i') mytime
FROM yourtabe";
$result = mysql_query($sql);
$dataarr = array();
if($result && mysql_num_row($result) > 0) {
while($row2 = mysql_fetch_assoc($result)) {
$dataarr[$row2['mydate']][] = $row2;
}
}
if(count($dataarr) > 0 )
foreach($dataarr as $key => $data) {
echo '<h3>'.$key.'</h3>';
foreach($data as $row) {
echo '<p>'. $row['id'].', '.$row['name'].', '.$row['mytime'].'</p>';
}
}
}
答案 1 :(得分:0)
$sql="SELECT extract(datetime) as date,id,name,time(datetime) as time from
tablename";
$result=mysql_query($sql);
上面的查询使用mysql日期和时间函数来了解更多信息,请参阅
答案 2 :(得分:0)
我不确定你到底要做什么,但这可以为你提供一个如何进行初步排序的基本例子:
select date(datetime), id, name, TIME_FORMAT(datetime, '%H:%i') from tmp order by datetime