› 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
StevenTong
V2EX  ›  MySQL

关于 Mysql 里面日期相关的非递增递减的排序

  •  
  •   StevenTong · Mar 18, 2015 · 5035 views
    This topic created in 4216 days ago, the information mentioned may be changed or developed.
    有活动两个字段 startDate 和 endDate 类型是TIMESTAMP

    要跟 CURRENT_TIME 比较

    顺序是 活动进行中>未开始>已结束


    select * from actions where ?????? order by ??????
    13 replies  •  2015-03-23 15:37:25 +08:00
    jhdxr
        1
    jhdxr  
       Mar 18, 2015   ❤️ 1
    select *, if(time() >= startData and time() <= endData, 1, if(time() < startData, 2, 3)) as o from actions where ?????? order by o

    我觉得你这样子想要一条sql写完性能会死的很惨
    StevenTong
        2
    StevenTong  
    OP
       Mar 18, 2015
    select actions.*,
    if(current_timestamp > actions.startDate and current_timestamp < actions.endDate,1,0) as status,
    if(current_timestamp < actions.startDate,2,0) as status,
    if(current_timestamp > actions.endDate,3,0) as status
    from actions, actions_members
    where actions_members.m_id = 3 and actions_members.status = "ok" and actions.id = actions_members.t_id
    order by status asc;


    可是3条if怎么写到一起呢?
    StevenTong
        3
    StevenTong  
    OP
       Mar 18, 2015
    原来是1L这样写
    StevenTong
        4
    StevenTong  
    OP
       Mar 18, 2015
    @jhdxr 怎么优化好?
    aqqwiyth
        5
    aqqwiyth  
       Mar 18, 2015
    变相的分组排序,你是不是还有一个隐性的需求没提出来

    union all 我觉得比较适合

    selct 活动进行中
    union all
    selct 未开始
    union all
    selct 已结束
    aqqwiyth
        6
    aqqwiyth  
       Mar 18, 2015
    这样可以对 活动进行中 / 结束 的进行排序
    jlnsqt
        7
    jlnsqt  
       Mar 18, 2015
    能否修改表结构?若可以增加一个活动的状态字段会更好。不可以就用UNION ALL
    StevenTong
        8
    StevenTong  
    OP
       Mar 19, 2015
    @jlnsqt 表结构可以随意更改

    状态字段可以根据时间自己更新吗?
    jhdxr
        9
    jhdxr  
       Mar 19, 2015
    @aqqwiyth union all的问题在于如果还想用limit n,m的话不大好做吧?
    jhdxr
        10
    jhdxr  
       Mar 19, 2015
    @StevenTong 得看你的需求,你是做类似于排行榜那样子确定只有一页的么?
    StevenTong
        11
    StevenTong  
    OP
       Mar 19, 2015
    @jhdxr 用户可以创建一项团队活动 其他用户可以加入
    我创建的 或者我加入的 所有的活动
    在APP中 比如有我的活动这一项需要显示所有的与我有关联的 得按照进行中 未开始 已过期排序

    就是这样的需求
    jhdxr
        12
    jhdxr  
       Mar 19, 2015
    @StevenTong 我个人建议你可以拆成3个独立的sql,先只查进行中的,不够再去查未开始的,最后去查过期的。这样子就是翻页是个问题。。。但因为你这业务对时间要求不强(不像拍卖之类的),所以在应用层面去做这个页码的计算应该也ok
    jlnsqt
        13
    jlnsqt  
       Mar 23, 2015
    @StevenTong 不好意思,好几天没上了,可以啊,可以用MySQL本身的定时器,也可以用程序去实现,每隔1秒执行一次修改,如果每秒的任务数量不多的话,这种方式还是可行的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2251 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:31 · PVG 09:31 · LAX 18:31 · JFK 21:31
    ♥ Do have faith in what you're doing.