V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
Ufo666
V2EX  ›  MySQL

mysql 数据库存日期一般用什么类型?

  •  
  •   Ufo666 · Jul 13, 2022 · 5140 views
    This topic created in 1385 days ago, the information mentioned may be changed or developed.

    我之前用的 date ,但是在做日期减法,或者前端传来的范围时间查找,总是要去转换成时间戳,然后再转换成 date 类型,然后才能查询,所以我在想能不能直接存 int 类型的时间戳会比较好。

    27 replies    2022-08-16 17:03:04 +08:00
    shakaraka
        1
    shakaraka  
    PRO
       Jul 13, 2022
    我都存时间戳
    sgq1128
        2
    sgq1128  
       Jul 13, 2022
    date ,更直观一点
    luman
        3
    luman  
       Jul 13, 2022
    时间范围查询直接给 mysql 传字符串也可以查
    bruce0
        4
    bruce0  
       Jul 13, 2022
    我一般都是用 bigint, 存时间戳, 好处是可以随便比较和计算时间, 不好的地方就是直接看表不知道时间,需要手动计算
    yousabuk
        5
    yousabuk  
       Jul 13, 2022 via iPhone
    @bruce0 这个再增加一列就很好了
    Ufo666
        6
    Ufo666  
    OP
       Jul 13, 2022
    @wunonglin 时间戳用 int 还是 timestamp
    luman
        7
    luman  
       Jul 13, 2022
    计算时间可以用 date_add
    c1273082756
        8
    c1273082756  
       Jul 13, 2022
    时间戳和 date 都存就完美解决
    lix7
        9
    lix7  
       Jul 13, 2022
    永远只存 bigint
    shakaraka
        10
    shakaraka  
    PRO
       Jul 13, 2022
    @Ufo666 #6 bigint
    R18
        11
    R18  
       Jul 13, 2022
    unsigned int
    bruce0
        12
    bruce0  
       Jul 13, 2022
    @yousabuk 虽然可以 但是感觉有点冗余, 真的要看的话,手动复制找个工具转一下,或者查询的的时候用 sql 函数转一下
    R18
        13
    R18  
       Jul 13, 2022
    时区真的很麻烦, 数据库有时区,程序有时区,系统还有时区。如果不存时间戳,改个时区牵扯的问题太多了。
    Rache1
        14
    Rache1  
       Jul 13, 2022
    考虑时区就是用 timestamp ,或者 int 、bigint 。

    如果要考虑时区,大于 1970 ,同时要考虑超过 2038 的时间,但小于 2106 ,可以考虑无符号的 int 、如果要超过 2106 年,就要考虑 bigint 了。

    1970 以前的时间,也得考虑 date 、datetime 了(不考虑时区的)
    wbrobot
        15
    wbrobot  
       Jul 13, 2022
    使用 datetime ,存储逻辑还是 timestamp->int, 可以 SQL 里面写 date 字符串直接比较
    cnoder
        16
    cnoder  
       Jul 13, 2022
    没有跨时区业务就 datetime,有就时间戳
    TAFMT
        17
    TAFMT  
       Jul 13, 2022
    datetime
    keppelfei
        18
    keppelfei  
       Jul 13, 2022
    @bruce0 有没有什么软件可以自动转一下?反正都是函数计算,如果有软件支持自动转化,那就美了。
    hingbong
        19
    hingbong  
       Jul 13, 2022
    TIMESTAMP 就行, 反正互联网项目也活不到 2038 年
    r4aAi04Uk2gYWU89
        20
    r4aAi04Uk2gYWU89  
       Jul 13, 2022
    datetime 和 timestamp 直接比较会有什么问题吗?为什么要转时间戳再比较
    mmdsun
        21
    mmdsun  
       Jul 13, 2022 via iPhone
    数据库:datetime
    Java 代码:LocalDateTime ( Java8 新日期类)

    有时区也没事,注解或者 config 可以自己转,spring boot 项目
    ragnaroks
        22
    ragnaroks  
       Jul 13, 2022
    int64 时间戳唯一选择,不要给自己找麻烦
    liaohongxing
        23
    liaohongxing  
       Jul 13, 2022
    写 php 我喜欢 int 存, 写 go 我喜欢 datetime 3 位精度带毫秒
    fox0001
        24
    fox0001  
       Jul 13, 2022
    datetime 类型,保存 UTC 时区的时间
    liudengchn
        25
    liudengchn  
       Jul 13, 2022
    datetime
    Ufo666
        26
    Ufo666  
    OP
       Jul 15, 2022
    谢谢大家,已经采取 bigint 了
    nosoxo
        27
    nosoxo  
       Aug 16, 2022
    @c1273082756 天才
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 86ms · UTC 02:50 · PVG 10:50 · LAX 19:50 · JFK 22:50
    ♥ Do have faith in what you're doing.