Results 1 to 7 of 7

Thread: Take out a random record. [Resolved]

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Take out a random record. [Resolved]

    I have to take out 5 records from a MySQL table. First the 3 last one, and then 2 random records. But no one of them can be the same record. With my current skills I can see 2 methods to do it.

    - I can store all the records that I have taken out, then use a while loop and take out a new random one untill I get a new one.

    - I can use NOT LIKE (or what the syntax is again) and list up the first record that I have taken out the first time in the second one, and then the two first in the 3rd one, and the 3 first in the 4th one, and so on.


    I can't see that any of them is a good solution. Maybe the second one is a bit faster, but that is all.

    Is it a better way?
    Last edited by NoteMe; Jun 21st, 2004 at 02:41 PM.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    A very slight tweak on the first one:
    make a random number, then keep making them until you have 5 different ones. then remove those records from the db. this will stop you having to do extra mysql stuff.
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Ohhh yeah..that was a good one. Will try that one.

    Thanks.
    ØØ

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Well, using the DISTINCT keyword would eliminate any duplicates, and then you could just pick randomly from those results.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by ober0330
    Well, using the DISTINCT keyword would eliminate any duplicates, and then you could just pick randomly from those results.

    Not sure if I got you. There is no duplicated entries in the table. The problem is if I first take out 3 records, then I want 2 random ones, then I can get one of those I have all ready picked.

  6. #6
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Well what sort of thing are you doing? Why not take 5 out and then when you want the extra 2 later, you already have them and you're not going to get ones you already picked?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by ober0330
    Well what sort of thing are you doing? Why not take 5 out and then when you want the extra 2 later, you already have them and you're not going to get ones you already picked?
    Becase it's cd and 12" reviews. So we are showing the picture and the few first words from the 3 latest, then we pick 3 good one that have got 6points, that we show off at the bottom. So I can't do it that way. It was to be done in two searches at minimum.

Posting Permissions

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



Click Here to Expand Forum to Full Width