MySQL統計函數記錄

來源:互聯網
上載者:User

按年匯總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y');

按月匯總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m');

按季度匯總,統計:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));

按小時:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%Y-%m-%d %H ');

查詢 本年度的資料:

SELECT * FROM mytable WHERE year(FROM_UNIXTIME(my_time)) = year(curdate())

查詢資料附帶季度數:

SELECT id, quarter(FROM_UNIXTIME(my_time)) FROM mytable;

查詢 本季度的資料:

SELECT * FROM mytable WHERE quarter(FROM_UNIXTIME(my_time)) = quarter(curdate());

本月統計:

select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate())

本周統計:

select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate())

N天內記錄:

WHERE TO_DAYS(NOW())-TO_DAYS(時間欄位)<=N

  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一頁
【內容導航】
第1頁:時間段統計 第2頁:GROUP_CONCAT()函數
第3頁:按條件計數 第4頁:Join串連詳解
第5頁:數值函數

聯繫我們

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