SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Randomize results then sort

    Say I am querying a MySQL database of profiles under a certain city but I want those results randomized and then from that randomized order, sort by user level (admin, general, etc). I have looked at multiple solutions online with ORDER BY and GROUP BY but whenever an ORDER BY RAND() query happens followed by GROUP BY user_level, it doesn't keep the admins at the top.

    So in a nutshell I am looking to randomize all profiles from table but put the admins in the result first but randomize the admins at the top in addition to randomizing the general users at the bottom. Is this possible? I am using PHP by the way.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT
      col1,
      col2, 
      col3,
      user_level
    FROM
      (SELECT col1, col2, col3, user_level FROM table ORDER BY RAND()) t1
    ORDER BY user_level


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •