php如何查詢某月或者某日有資料且輸出(文章歸檔功能)

來源:互聯網
上載者:User
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` < 7月1號0點時間戳記
------解決方案--------------------
探討
SELECT count(1) FROM `articles` WHERE `pubtime` > 6月1號0點時間戳記 and `postdate` < 7月1號0點時間戳記

------解決方案--------------------
SQL code
>select count(1), date(pubtime) from articles group by date(pubtime);
------解決方案--------------------
先把幾個點的時間戳記取出來.如2012年8月、2012年6月。
然後在拼接sql查詢,應該很容易的。
別著急,你試試
------解決方案--------------------
1、
select FROM_UNIXTIME(pubtime, '%Y-%m') as pubtime, count(*) as cnt from articles group by FROM_UNIXTIME(pubtime, '%Y-%m')

------解決方案--------------------
這是每個月:
SQL code
>select count(1), extract(year_month from pubtime) from articles  group by extract(year_month from pubtime);
------解決方案--------------------
有個問題,在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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.