標籤:
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初識(五) 統計與計算與時間