Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.73 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Voting/Ranking logic with SQL
  2. select
  3.     a.id,
  4.     (
  5.         select
  6.             min(c.id)
  7.         from
  8.             table1 c
  9.         where
  10.             c.id>a.id
  11.         limit 1
  12.     ) as id2
  13. from
  14.     table1 a
  15. order by
  16.     rand()
  17. limit 1;
  18.        
  19. select
  20.     b.id
  21. from
  22.     table1 b,
  23.     (
  24.     select
  25.         a.id as id
  26.     from
  27.         table1 a
  28.     where
  29.         a.id<(select max(id) from table1 limit 1)
  30.     order by
  31.         rand()
  32.     limit 1
  33.     ) a
  34. where
  35.     b.id>=a.id
  36.     order by
  37.     b.id
  38. limit 2
  39. ;
  40.        
  41. with first as (select id
  42.                from table t
  43.                where id < (select max(id) from table)
  44.                order by rand()
  45.                limit 1
  46.               )
  47. select id
  48. from table t
  49. order by abs(id - first.id)
  50. limit 2