Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1867|回复: 2
打印 上一主题 下一主题

基于毫秒级日期的数据库字段设计

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2017-3-30 14:52:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 java 于 2017-11-23 11:15 编辑
  1. CREATE TABLE `test_keys` (
  2.   `id` INT(11) NOT NULL AUTO_INCREMENT
  3.   `name` VARCHAR(64) NOT NULL COMMENT '名称'
  4.   `create_time` BIGINT(20) DEFAULT NULL,
  5.   `modify_time` BIGINT(20) DEFAULT NULL,
  6.   PRIMARY KEY (`id`),
  7.   INDEX `idx_classification` (`modify_time` ASC)
  8. ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8 COLLATE = UTF8_BIN;


  9. insert into test_keys(name, create_time, modify_time) values('xxxx', UNIX_TIMESTAMP(now(3))*1000, UNIX_TIMESTAMP(now(3))*1000);

  10. select from_unixtime(modify_time/1000) from test_keys;
复制代码
update ad set begin_time = unix_timestamp('2016-01-02 12:12:12.001')*1000 where id = 55;
select from_unixtime(begin_time/1000), begin_time from ad where id = 55;    查看结果-- '2016-01-02 12:12:12.0010', '1451707932001'

select CONCAT('''', order_id) as order_id, FROM_UNIXTIME(modify_time/1000) as modify_time,FROM_UNIXTIME(modify_time/1000,'%Y-%c-%d %h:%i:%s') as timedd  from v_order order by modify_time desc limit 0,10
回复

使用道具 举报

55

主题

78

帖子

289

积分

认证用户组

Rank: 5Rank: 5

积分
289
沙发
发表于 2017-4-11 16:12:04 | 只看该作者
本帖最后由 linux 于 2017-4-11 18:13 编辑
  1. <resultMap id="BaseResultMap" type="com.ktionKeys"
  2.     <result column="create_time" jdbcType="BIGINT" property="createTime" />
  3.     <result column="modify_time" jdbcType="BIGINT" property="modifyTime" />
  4.   </resultMap>
复制代码
  1. <table id="welfareDetailTable" class="table">
  2.         <thead>
  3.         <tr>
  4.             <th>标题</th>
  5.             <th>编码</th>
  6.             <th>状态</th>
  7.             <th>创建时间</th>
  8.             <th>修改时间</th>
  9.             <th style="width: 200px;"></th>
  10.         </tr>
  11.         </thead>
  12.         <tbody>
  13.         <#list welfareBvo.welfareList as welfare>
  14.         <tr>
  15.             <td>${(welfare.title)!''}</td>
  16.             <td>${(welfare.code)!''}</td>
  17.             <td>${(welfare.status)!''}</td>
  18.             <td> ${(welfare.createTime)?number_to_datetime}</td>
  19.             <td> ${(welfare.modifyTime)?number_to_datetime}</td>
  20.             <td>
  21.                 <a href="javascript:void(0);" onclick="updateWelfare(${welfare.id})" title="修改"><i class="icon-pencil"></i></a> |
  22.                 <a href="javascript:void(0);" onclick="deleteWelfare(${welfare.id})" title="删除"><i class="icon-remove"></i></a>
  23.             </td>
  24.         </tr>
  25.         </#list>
  26.         </tbody>
  27.     </table>
复制代码
回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
板凳
 楼主| 发表于 2017-6-29 09:48:42 | 只看该作者
本帖最后由 java 于 2017-6-29 09:50 编辑

涉及的函数date_format(date, format) 函数,MySQL日期格式化函数date_format()
unix_timestamp() 函数
str_to_date(str, format) 函数
from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime
时间转字符串
  • select date_format(now(), '%Y-%m-%d');  
  • #结果:2016-01-05

时间转时间戳
  • select unix_timestamp(now());  
  • #结果:1452001082

字符串转时间
  • select str_to_date('2016-01-02', '%Y-%m-%d %H');  
  • #结果:2016-01-02 00:00:00

字符串转时间戳
  • select unix_timestamp('2016-01-02');  
  • #结果:1451664000

时间戳转时间
  • select from_unixtime(1451997924);  
  • #结果:2016-01-05 20:45:24

时间戳转字符串
  • select from_unixtime(1451997924,'%Y-%d');  
  • //结果:2016-01-05 20:45:24

附表
[size=1em]MySQL日期格式化(format)取值范围。
含义
%S、%s两位数字形式的秒( 00,01, ..., 59)
%I、%i两位数字形式的分( 00,01, ..., 59)
小时 %H24小时制,两位数形式小时(00,01, ...,23)
%h12小时制,两位数形式小时(00,01, ...,12)
%k24小时制,数形式小时(0,1, ...,23)
%l12小时制,数形式小时(0,1, ...,12)
%T24小时制,时间形式(HH:mm:ss)
%r 12小时制,时间形式(hh:mm:ss AM 或 PM)
%p AM上午或PM下午
  周 %W一周中每一天的名称(Sunday,Monday, ...,Saturday)
%a一周中每一天名称的缩写(Sun,Mon, ...,Sat)
%w 以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday)
%U数字表示周数,星期天为周中第一天
%u数字表示周数,星期一为周中第一天
%d 两位数字表示月中天数(01,02, ...,31)
%e 数字表示月中天数(1,2, ...,31)
%D英文后缀表示月中天数(1st,2nd,3rd ...)
%j以三位数字表示年中天数(001,002, ...,366)
%M 英文月名(January,February, ...,December)
%b 英文缩写月名(Jan,Feb, ...,Dec)
%m 两位数字表示月份(01,02, ...,12)
%c 数字表示月份(1,2, ...,12)
%Y 四位数字表示的年份(2015,2016...)
%y 两位数字表示的年份(15,16...)
文字输出 %文字 直接输出文字内容

http://www.cnblogs.com/jhy-ocean/p/5560857.html








回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-19 02:38 , Processed in 0.059936 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表