MySQL时间函数from_unixtime()date_format()unix_timestamp()now()使用说明
MySQL时间函数from_unixtime()date_format()unix_timestamp()now()使用说明
now() 当前时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-09-10 19:20:19 |
+---------------------+
1
2
3
4
5
6
unix_timestamp() 当前时间戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1536578429 |
+------------------+
1
2
3
4
5
6
unix_timestamp(now()) 当前时间转换成时间戳
mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
| 1536578445 |
+-----------------------+
1
2
3
4
5
6
unix_timestamp(‘2018-08-08’) 指定时间转换成时间戳
mysql> select unix_timestamp('2018-08-08');
+------------------------------+
| unix_timestamp('2018-08-08') |
+------------------------------+
| 1533657600 |
+------------------------------+
1
2
3
4
5
6
from_unixtime(1533657600) 将时间戳转换为时间 默认格式 ‘%Y-%m-%d %H:%i:%s’
mysql> select from_unixtime(1533657600);
+---------------------------+
| from_unixtime(1533657600) |
+---------------------------+
| 2018-08-08 00:00:00 |
+---------------------------+
1
2
3
4
5
6
from_unixtime(1533657600, ‘%Y-%m-%d’) 将时间戳转换为时间 并进行格式化
mysql> select from_unixtime(1533657600, '%Y-%m-%d');
+---------------------------------------+
| from_unixtime(1533657600, '%Y-%m-%d') |
+---------------------------------------+
| 2018-08-08 |
+---------------------------------------+
1
2
3
4
5
6
查询时间戳字段 转换为时间格式显示
mysql> select from_unixtime(ctime, '%Y-%m-%d') from logs limit 1;
+----------------------------------+
| from_unixtime(ctime, '%Y-%m-%d') |
+----------------------------------+
| 2018-09-18 |
+----------------------------------+
1
2
3
4
5
6
where时间戳字段
mysql> select count(*) from logs where from_unixtime(ctime, '%Y%m%d')=20180808 limit 1;
+----------+
| count(*) |
+----------+
| 12345 |
+----------+
1
2
3
4
5
6
date_format() 将时间进行格式化显示
mysql> select date_format(now(), '%Y/%m/%d');
+--------------------------------+
| date_format(now(), '%Y/%m/%d') |
+--------------------------------+
| 2018/08/08 |
+--------------------------------+
1
2
3
4
5
6
where时间字段
mysql> select count(*) from logs where date_format(cdate, '%Y%m%d')=20180910 limit 1;
+----------+
| count(*) |
+----------+
| 123 |
+----------+
————————————————
版权声明:本文为CSDN博主「gocuber」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gocuber/article/details/80195591
相关文章
- MySQL脚本教程:快速学习数据库操作(mysql脚本教程)
- 深入浅出Mysql统计函数(mysql统计函数)
- MySQL使用手册:快速免费下载(mysql使用手册下载)
- MySQL 集群: 卓越的工具体验 (mysql集群工具)
- MySQL查询:排名展示我的成绩(mysql查询个人排名)
- 及其应用MySQL:原理与应用(数据库mysql原理)
- MySQL中毫秒格式化的实现(mysql毫秒格式化)
- 的查询MySQL:查询精确到毫秒的时间决定性结果(mysql精确到毫秒)
- MySQL命令技巧:用替换减少编码时间(mysql命令替换)
- 掌握MySQL时间格式化技巧(mysql时间格式化)
- MySQL中的小技巧:数据类型精选(mysql中数据类型)
- MySQL查询和使用时间戳的注意事项(mysql查询时间戳)
- 深入MySQL查看当前数据库状态(mysql查看当前数据库)
- MySQL默认值:时间的最佳选择(mysql默认值时间值)
- MySQL日期最大值max函数(mysql中max 日期)
- 利用MySQL中的DAY函数获取日期信息(mysql中day 函数)
- MySQL中的CEIT函数使用详解(mysql中CEIT)
- 2003年MySQL安装指南(2003 mysql安装)
- MySQL中关联索引的作用和实现(mysql中关联索引)
- 解析MySQL在2021年一月的发展趋势与应用新技巧(mysql 一月)