mysql初識(五) 統計與計算與時間

來源:互聯網
上載者:User

標籤:

mysql 日期與時間


*/
擷取當前日期和時間
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2015-10-28 19:51:17 |
+---------------------+

擷取當前日期
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2015-10-28 |
+------------+

擷取目前時間
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 19:57:01 |
+-----------+

datetime() 擷取日期和時間
date() 獲得日期
time() 獲得時間
year()年
month()月
day() 天
hour()時
minute() 分
second()秒

以上這些都是截取格式 像這樣
mysql> select year(‘20150303‘);
+------------------+
| year(‘20150303‘) |
+------------------+
| 2015 |
+------------------+
mysql> select date(‘20150303‘);
+------------------+
| date(‘20150303‘) |
+------------------+
| 2015-03-03 |
+------------------+

 

/**
mysql 統計與計算(這個比較重要)


*/
統計有多少條資料
mysql> select count(*) from stuinfo;
獲得年齡最小值
mysql> select min(age) from stuinfo;
獲得最大值用max
mysql> select max(age) from stuinfo;
獲得總和是用sum()
mysql> select sum(age) from stuinfo;
獲得平均數
mysql> select avg(age) from stuinfo;

以上這些都是服務於分組的 group by ... having ...
分組計算男女同學的總數
mysql> select sex,count(*) from stuinfo group by sex;
+------+----------+
| sex | count(*) |
+------+----------+
| 男 | 3 |
| 女 | 3 |
+------+----------+

having是配合group by使用的,組合完畢後再添加分組條件
mysql> select sex,count(*) from stuinfo group by sex having sex=‘男‘;
+------+----------+
| sex | count(*) |
+------+----------+
| 男 | 3 |
+------+----------+

 

mysql初識(五) 統計與計算與時間

聯繫我們

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