• 请不要在回答技术问题时复制粘贴 AI 生成的内容
rqxiao
V2EX  ›  程序员

mysql datetime 字段进行比较

  •  
  •   rqxiao · Oct 17, 2019 · 2973 views
    This topic created in 2403 days ago, the information mentioned may be changed or developed.

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

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

    剩下的 返回已失效

    求解

    3 replies    2019-10-18 10:49:05 +08:00
    nwljy
        1
    nwljy  
       Oct 18, 2019
    case when

    当前时间 :SYSDATE;
    格式 (to_date(BUSI_DATE,'yyyyMMdd') + 8) >= SYSDATE;
    rqxiao
        2
    rqxiao  
    OP
       Oct 18, 2019
    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  
       Oct 18, 2019   ❤️ 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 的时候会多一个字段, 处理的时候忽略就可以了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2869 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 12:26 · PVG 20:26 · LAX 05:26 · JFK 08:26
    ♥ Do have faith in what you're doing.