English 中文(简体)
MySQL + PHP(按日期分组)
原标题:MySQL + PHP (grouping by date)

I m试图在数据库中储存的日期(unix timest)列出不同的斜体。

我需要获得MySQL(频率)和PHP(产出)的帮助。

http://www.ohchr.org。

id | subject | time
1 | test1 | 1280278800
2 | test2 | 1280278800
3 | test3 | 1280365200
4 | test4 | 1280451600
5 | test5 | 1280451600

<>光>

Today
test5
test4

Yesterday
test3

July 28
test2
test1

我对此表示赞赏。 感谢!

最佳回答

您可使用<代码>DATE(FROM_UNIXTIME(time),将您的不准确的时间序列转换为日期。 这将产生诸如<代码>2010-07-30等内容。

下表按日期分列。

SELECT id, subject, time, DATE(FROM_UNIXTIME(time)) AS date_column
GROUP BY date_column

<><>Edit>: 页: 1

为此,我只操作一个标准<代码>。

然后与PHP进行分类。

$lastDate = null;

foreach ($rows as $row) {
    $date = date( Y-m-d , $row[ time ]);
    $time = date( H:i , $row[ time ]);

    if (is_null($lastDate) || $lastDate !== $date) {
        echo "<h2>{$date}</h2>";
    }

    echo "{$time}<br />";

    $lastDate = $date;
}
问题回答

你们可以为此创造我的智慧。

DELIMITER $$

DROP FUNCTION IF EXISTS `niceDate` $$
CREATE FUNCTION `niceDate` (ts INT) RETURNS VARCHAR(255) NO SQL
BEGIN

  declare dt DATETIME;
  declare ret VARCHAR(255);

  set dt = FROM_UNIXTIME(ts);

  IF DATE_FORMAT(dt, "%Y%m%d") = DATE_FORMAT(NOW(), "%Y%m%d") THEN SET ret =  Today ;
  ELSEIF DATE_FORMAT(dt, "%Y%m%d") = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), "%Y%m%d") THEN SET ret =  Yesterday ;
  ELSE SET ret = CONCAT(DATE_FORMAT(dt, "%M "), DATE_FORMAT(dt, "%d"));
  END IF;

  RETURN ret;

END $$

DELIMITER ;

You could then construct your query like this: select niceDate(your_date_field) from table group by niceDate(your_date_field) order by your_date_field desc

否认: i 先检查了这一功能,但你应当了解这一想法。

最容易的方式可能是按时间顺序排列(代号),并在项目组合中进行: 在设定结果之后,将时间段改为日标;当目前日期与前一日期不同时(hint:使用日期:format()),插入一个小标题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签