записал заготовку для пагинации)
                  
                  
                  select * from todos
                  
                  
                     where date >= CURRENT_DATE and city_id=1
                  
                  
                     order by date asc;
                  
                  
                  if not found then
                  
                  
                    select * from todos 
                  
                  
                    where date < CURRENT_DATE and city_id=1
                  
                  
                    order by date desc;
                  
                  
                  end if;
                  
                  
                
очень неоптимально (делал на tsql, могут быть ошибки на пг диалекте) select * /* исключить flag если что */ from (select 1 as flag, * from todos where date >= CURRENT_DATE and city_id=1 union all select 2 as flag, * from todos where date < CURRENT_DATE and city_id=1 ) s where ( s.flag =2 and not exists (select 1 from todos where date >= CURRENT_DATE and city_id=1) or ( s.flag =1 and exists (select 1 from todos where date >= CURRENT_DATE and city_id=1) order by date desc
Пагинацию делай на клиенте. Запрос будет легче в разы, и правильней
Обсуждают сегодня