SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mySQL unique entries question

    Hey guys

    Just a quick one

    In my database I have a number of entries that are assigned to different sections (ie 3 entries to section1, 5 entries to section 2, 4 entries to section 3)

    What I want to do is run a mysql query that will select 1 entry from each of the 'sections' and return the result

    ie so i end up with 1 random entry with a section ID of section 1, 1 random entry with a section ID of section 2 etc etc

    Any ideas?

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    the simplest thing to do is this:
    Code mysql:
    select section
         , something
      from some_table
     where section = 1
    order
        by rand()
     limit 1
     union all
    select section
         , something
      from some_table
     where section = 2
    order
        by rand()
     limit 1


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
  •