0

我有一个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
4

3 回答 3

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>'; 
        }
    }
}
于 2012-11-15T09:03:43.020 回答
0
$sql="SELECT extract(datetime) as date,id,name,time(datetime) as time from
       tablename";
$result=mysql_query($sql);

上面的查询使用 mysql date 和 tme 函数来了解它们更多见

http://phpdynamic.blogspot.in/2012/11/php-myssql.html

于 2012-11-15T09:12:35.933 回答
0

我不确定你到底要做什么,但这可以给你一个关于如何进行初始排序的基本示例:

select date(datetime), id, name, TIME_FORMAT(datetime, '%H:%i') from tmp order by datetime

于 2012-11-15T09:44:28.127 回答