› 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 · 5709 views
    This topic created in 1576 days ago, the information mentioned may be changed or developed.

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

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

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

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

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