V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rqxiao
V2EX  ›  程序员

mysql datetime 字段进行比较

  •  
  •   rqxiao · 2019-10-17 18:20:34 +08:00 · 2126 次点击
    这是一个创建于 1624 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果大于当前时间 返回待生效

    如果小于当前时间 且是离当前时间最近的 返回 使用中

    剩下的 返回已失效

    求解

    3 条回复    2019-10-18 10:49:05 +08:00
    nwljy
        1
    nwljy  
       2019-10-18 03:03:27 +08:00
    case when

    当前时间 :SYSDATE;
    格式 (to_date(BUSI_DATE,'yyyyMMdd') + 8) >= SYSDATE;
    rqxiao
        2
    rqxiao  
    OP
       2019-10-18 09:30:35 +08:00
    SELECT
    effective_time ,
    CASE
    WHEN effective_time <date_format(now() ,'%Y-%m-%d %H:%i:%S') THEN
    '11'
    WHEN effective_time >date_format(now() ,'%Y-%m-%d %H:%i:%S') THEN
    '22'
    WHEN effective_time =date_format(now() ,'%Y-%m-%d %H:%i:%S') THEN
    '33'
    ELSE
    '44444444'
    END 'xxxx'
    FROM
    student;



    select max(effective_time) from student where effective_time< now();


    请问有什么办法把下面 那句话放到 第三个 when 里面吗
    setsunakute
        3
    setsunakute  
       2019-10-18 10:49:05 +08:00   ❤️ 1
    SELECT
    effective_time , @max_time := (select max(effective_time) from student where create_time < now()) as 'max_time',
    CASE
    WHEN effective_time < @max_time THEN
    '11'
    WHEN effective_time > @max_time THEN
    '22'
    WHEN effective_time = @max_time THEN
    '33'
    ELSE
    '44444444'
    END 'xxxx'
    FROM
    student;


    这样, select 的时候会多一个字段, 处理的时候忽略就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1137 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:34 · PVG 02:34 · LAX 11:34 · JFK 14:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.