mysql初识(五) 统计与计算与时间

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 显示年份
举例:
create table birthday(
id int primary key auto_increment,
birth datetime
);
就和char int之类的用法一样

 

一下是查询是的处理函数
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 |
+------+----------+

 

posted @ 2015-10-29 10:30  吾辈丶何以为战  阅读(165)  评论(0编辑  收藏  举报