求教一個資料庫問題,有張表,裡面有每天存入的資料,怎麼取出每個月資料的條數。

來源:互聯網
上載者:User
這是ec_ip_article表,iptime是每天的時間戳記。有沒有簡便一點的方法,不想每個月一條條sql取出來,然後再count得出數目,那樣sql太多了。


回複討論(解決方案)

select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1

select FROM_UNIXTIME(iptime,'%Y%m%d'),count(0) from ec_ip_article group by FROM_UNIXTIME(iptime,'%Y%m%d')

select FROM_UNIXTIME(iptime,'%Y%m%d'),count(0) from ec_ip_article group by FROM_UNIXTIME(iptime,'%Y%m%d')



select FROM_UNIXTIME(iptime,'%Y%m'),count(0) from ec_ip_article group by FROM_UNIXTIME(iptime,'%Y%m')

SELECT ip_id,count(ip_id) count,FROM_UNIXTIME(iptime, '%Y%m%d') date FROM test GROUP BY FROM_UNIXTIME(iptime, '%c') ORDER BY iptime

select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1

裡面的%Y-%m是時間戳記嗎?

這是ec_ip_article表,iptime是每天的時間戳記。有沒有簡便一點的方法,不想每個月一條條sql取出來,然後再count得出數目,那樣sql太多了。

$sql="select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1  where  user_id=392";$result=mysql_query($sql);$row=mysql_fetch_row($result);dump($row);
為什麼取不出來

select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1

$sql="select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1  where  user_id=392";$result=mysql_query($sql);$row=mysql_fetch_row($result);dump($row);
為什麼取不出來

不可能!你是怎麼寫的?

測試例

mysql_connect();mysql_select_db('test');mysql_query("create temporary table ec_ip_article ( iptime int )");$a = array('2015-02-10', '2015-02-20', '2015-03-10','2015-04-10','2015-04-21',);foreach($a as $t)mysql_query(sprintf("insert into ec_ip_article values (%d)", strtotime($t)));$sql = "select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1";$rs = mysql_query($sql);while($r = mysql_fetch_assoc($rs))  echo join(' ', $r), PHP_EOL;
2015-02 22015-03 12015-04 2

測試例

mysql_connect();mysql_select_db('test');mysql_query("create temporary table ec_ip_article ( iptime int )");$a = array('2015-02-10', '2015-02-20', '2015-03-10','2015-04-10','2015-04-21',);foreach($a as $t)mysql_query(sprintf("insert into ec_ip_article values (%d)", strtotime($t)));$sql = "select FROM_UNIXTIME(iptime,'%Y-%m') as m, count(*) as cnt from ec_ip_article group by 1";$rs = mysql_query($sql);while($r = mysql_fetch_assoc($rs))  echo join(' ', $r), PHP_EOL;
2015-02 22015-03 12015-04 2

ec_ip_article表如果後面有個使用者ID欄位,然後統計每個使用者每個月的次數時直接在後面加where不行啊
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.