Guest User

Untitled

a guest
Aug 10th, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment