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