Home  >  Article  >  Backend Development  >  PHP +MySQL 如何时间按天数分页?

PHP +MySQL 如何时间按天数分页?

WBOY
WBOYOriginal
2016-06-06 20:20:321370browse

mysql 中有a表,有字段time,时间戳格式

如果数统计出记录跨度的天数,比如今天有n条记录产生,2015年12月1号,有m条记录产生,那么统计出来应该得到数是2,只为记录只 今天,和 2015年12月1号 这两天产生过记录。

比如今天有记录,昨天有记录,还有2015年12月1号 有记录,如何找出这‘前三天’的所有数据

.....后来自己想到一种...............................................

<code>mysql> SELECT FROM_UNIXTIME(inputtime,'%Y%m%d') day,COUNT(id) COUNT FROM c_content GROUP BY day order by day desc ;
+----------+-------+
| day      | COUNT |
| 20160111 |    28 |
| 20160109 |    41 |
| 20160108 |    44 |
| 20160107 |    52 |
| 20160106 |    42 |
| 20160105 |    49 |
| 20151229 |    39 |
| 20151228 |    35 |
+----------+-------+
24 rows in set</code>

得到数组 ,PHP用 count(array) 可以算总天数

<code>mysql> SELECT FROM_UNIXTIME(inputtime,'%Y%m%d') day,COUNT(id) COUNT FROM c_content GROUP BY day order by day desc limit 0,3;

+----------+-------+
| day      | COUNT |
| 20160111 |    28 |
| 20160109 |    41 |
| 20160108 |    44 |
+----------+-------+
3 rows in set</code>

得到“前三天”统计情况, 取出数组 头 (20160111 ),尾(20160108 ) 转成时间戳,

<code>$start ,$end;

SELECT * FROM c_content where inputtime between $end and $start;</code>

回复内容:

mysql 中有a表,有字段time,时间戳格式

如果数统计出记录跨度的天数,比如今天有n条记录产生,2015年12月1号,有m条记录产生,那么统计出来应该得到数是2,只为记录只 今天,和 2015年12月1号 这两天产生过记录。

比如今天有记录,昨天有记录,还有2015年12月1号 有记录,如何找出这‘前三天’的所有数据

.....后来自己想到一种...............................................

<code>mysql> SELECT FROM_UNIXTIME(inputtime,'%Y%m%d') day,COUNT(id) COUNT FROM c_content GROUP BY day order by day desc ;
+----------+-------+
| day      | COUNT |
| 20160111 |    28 |
| 20160109 |    41 |
| 20160108 |    44 |
| 20160107 |    52 |
| 20160106 |    42 |
| 20160105 |    49 |
| 20151229 |    39 |
| 20151228 |    35 |
+----------+-------+
24 rows in set</code>

得到数组 ,PHP用 count(array) 可以算总天数

<code>mysql> SELECT FROM_UNIXTIME(inputtime,'%Y%m%d') day,COUNT(id) COUNT FROM c_content GROUP BY day order by day desc limit 0,3;

+----------+-------+
| day      | COUNT |
| 20160111 |    28 |
| 20160109 |    41 |
| 20160108 |    44 |
+----------+-------+
3 rows in set</code>

得到“前三天”统计情况, 取出数组 头 (20160111 ),尾(20160108 ) 转成时间戳,

<code>$start ,$end;

SELECT * FROM c_content where inputtime between $end and $start;</code>

123456789

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn