What is the SQL for 'next' and 'previous' in a table?

后端 未结 7 859
抹茶落季
抹茶落季 2020-12-05 12:21

I have a table of items, each of which has a date associated with it. If I have the date associated with one item, how do I query the database with SQL to get the \'previous

相关标签:
7条回答
  • 2020-12-05 12:49

    Firstly, this should work (the ORDER BY is important):

    select min(a)
    from theTable
    where a > 8
    
    select max(a)
    from theTable
    where a < 8
    

    For the second question that I begged you to ask...:

        select * 
        from theTable
        where date = 8
    
        union all
    
        select *
        from theTable
        where key = (select min(key) 
                     from theTable
                     where key > (select max(key)
                                  from theTable
                                  where date = 8)
                    )
    
        union all
    
        select *
        from theTable
        where key = (select max(key) 
                     from theTable
                     where key < (select min(key)
                                  from theTable
                                  where date = 8)
                    )
    
        order by key
    
    0 讨论(0)
提交回复
热议问题