Home  >  Article  >  Backend Development  >  php如何查询某月或者某日有数据且输出(文章归档功能)

php如何查询某月或者某日有数据且输出(文章归档功能)

WBOY
WBOYOriginal
2016-06-13 10:37:461139browse

php怎么查询某月或者某日有数据且输出(文章归档功能)
有 articles表

id class title content pubtime
1 10 标题1 内容1 1342756599
2 11 标题2 内容2 1339392385
3 12 标题3 内容3 1339390661
4 10 标题4 内容4 1339139926
5 13 标题5 内容5 1339139892
6 16 标题6 内容6 1342756624

希望查询的结果是:
1.如2012年8月、2012年6月内有文章,输出一次 2012年8月、2012年6月的链接,最好能计算出汇总,有多少篇文章。
2.如2012年7月16日、2012年7月28日有文章 则倒序输出 2012年7月28日、2012年7月16日的链接,最好能计算出汇总,有多少篇文章。

不会写,给个思路也行啊。

------解决方案--------------------

SQL code
SELECT count(1) FROM `articles` WHERE `pubtime` > 6月1号0点时间戳 and `postdate` <font color="#e78608">------解决方案--------------------</font><br>
探讨
SELECT count(1) FROM `articles` WHERE `pubtime` > 6月1号0点时间戳 and `postdate`

------解决方案--------------------
SQL code
>select count(1), date(pubtime) from articles group by date(pubtime);<br><font color="#e78608">------解决方案--------------------</font><br>先把几个点的时间戳取出来.如2012年8月、2012年6月。<br>然后在拼接sql查询,应该很容易的。 <br>别着急,你试试<br><font color="#e78608">------解决方案--------------------</font><br>1、<br>select FROM_UNIXTIME(pubtime, '%Y-%m') as pubtime, count(*) as cnt from articles group by FROM_UNIXTIME(pubtime, '%Y-%m')<br><br><font color="#e78608">------解决方案--------------------</font><br>这是每个月:<br>
SQL code
>select count(1), extract(year_month from pubtime) from articles  group by extract(year_month from pubtime);<br><font color="#e78608">------解决方案--------------------</font><br>有个问题,在sql中使用函数会影响sql的执行效率,被用字段的主键什么的,有时候也没效果
探讨

这是每个月:
SQL code
>select count(1), extract(year_month from pubtime) from articles group by extract(year_month from pubtime);

引用:

SQL code
>select count(1), date(pubtime) from article……

------解决方案--------------------
探讨
我怎么能一次性查出 2012年6月 和 2012年8月
一年有12个月 我不能每个月都写一个语句吧,我想一次性查询一个月每一天是否有文章 一个月30天呢
就是这儿 怎么做呢

------解决方案--------------------
SQL code
SELECTSUM(CASE when MONTH( FROM_UNIXTIME(pubtime, '%Y-%m-%d'))=6 THEN 1 ELSE 0 END)AS sum_6,SUM(CASE when MONTH( FROM_UNIXTIME(pubtime, '%Y-%m-%d'))=8 THEN 1 ELSE 0 END)AS sum_8FROM dc_admin<div class="clear">
                 
              
              
        
            </div>
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