V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
jakeyfly
V2EX  ›  问与答

mysql 中 如何存入不要秒的日期格式:年-月-日-时-份 不要秒。或者直接月-日-时-分,因为对年份不敏感。

  •  
  •   jakeyfly · Feb 26, 2018 · 4207 views
    This topic created in 2993 days ago, the information mentioned may be changed or developed.

    有想过字符串,可是这个日期提取的时候需要比对,字符串不好比对吧?求大佬们赐教

    10 replies    2018-02-26 21:00:30 +08:00
    yangqi
        1
    yangqi  
       Feb 26, 2018
    存 datetime, 提取对比的时候忽略秒不就好了。
    jakeyfly
        2
    jakeyfly  
    OP
       Feb 26, 2018
    @yangqi 插入的时候不写秒都是 00 是吧
    yangqi
        3
    yangqi  
       Feb 26, 2018
    @jakeyfly 那也可以,不过要我就插入正常插入。读取之后你要怎么处理在程序里面处理就好了。
    abusizhishen
        4
    abusizhishen  
       Feb 26, 2018 via Android
    忽略就好
    cxh116
        5
    cxh116  
       Feb 26, 2018 via Android
    把年和秒改成一个固定值
    比如 1960 年 00 秒
    MeteorCat
        6
    MeteorCat  
       Feb 26, 2018 via Android
    时间戳 int 范围取值就行了,还容易触发索引,where datetime < xxxxxx and datetime > xxxxxx,这样来处理就行,也能对时间做 group by 处理,后续如果突然项目需要完全日期时间也能直接转换,能够适应各种变化
    justfindu
        7
    justfindu  
       Feb 26, 2018
    这样的需求为啥不用 varchar 来存
    SourceMan
        8
    SourceMan  
       Feb 26, 2018   ❤️ 1
    数据库应该保持数据的完整性
    前端显示什么应该由前端和后端来格式化
    jakeyfly
        9
    jakeyfly  
    OP
       Feb 26, 2018 via iPhone
    @SourceMan 大佬 明白了
    cxbig
        10
    cxbig  
       Feb 26, 2018 via iPad
    存 timestamp,int(10),省空间好排序
    显示的时候前端处理一下就好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   942 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 20:27 · PVG 04:27 · LAX 13:27 · JFK 16:27
    ♥ Do have faith in what you're doing.