Random select

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wim Roffil

    #1

    Random select

    Hi,

    When I do a select with a limit I get always the same records. Usually this
    is the desidered effect.

    But now I want to do a random select. So every time I do a select I should
    get a different set of results.

    What is the best way to achieve this effect?

    Thanks,
    Wim


  • Randell D.

    #2
    Re: Random select

    Wim Roffil wrote:[color=blue]
    > Hi,
    >
    > When I do a select with a limit I get always the same records. Usually this
    > is the desidered effect.
    >
    > But now I want to do a random select. So every time I do a select I should
    > get a different set of results.
    >
    > What is the best way to achieve this effect?
    >
    > Thanks,
    > Wim
    >
    >[/color]

    What is your problem? Do you really want random records, or are you
    bothered that you get the same records everytime you perform a select?

    If its the latter, you should use a WHERE clause in your SELECT and
    perhaps also a LIMIT start,rows

    Thus,something like

    SELECT firstname,lastn ame FROM myTable WHERE ID>10 LIMIT 5,12

    will find all records that have ID>10 and return 12 records, starting
    from the 5th one found (thus something like records with ID 15 to 27).

    If you want to read random records, request a random record (I'm
    assuming you've got a column/cell that is AUTO_INCREMENT that you could
    use as reference).

    Does that help you any?
    randelld

    Comment

    • Wim Roffil

      #3
      Re: Random select


      "Randell D." <reply.via.news .group.only.tha nks@fiprojects. moc> schreef in
      bericht news:LpHQd.4075 06$8l.401215@pd 7tw1no...[color=blue]
      > Wim Roffil wrote:[color=green]
      > > Hi,
      > >
      > > When I do a select with a limit I get always the same records. Usually[/color][/color]
      this[color=blue][color=green]
      > > is the desidered effect.
      > >
      > > But now I want to do a random select. So every time I do a select I[/color][/color]
      should[color=blue][color=green]
      > > get a different set of results.
      > >
      > > What is the best way to achieve this effect?
      > >
      > > Thanks,
      > > Wim
      > >
      > >[/color]
      >
      > What is your problem? Do you really want random records, or are you
      > bothered that you get the same records everytime you perform a select?[/color]

      Thank you for your reply.

      I need reaaly random records. From a big collection I want to show the
      visitors of my webpage just 5 or 10. Because adjacent records may be related
      they should be spread over the whole collection. AUTO_INCREMENT is not an
      option: it is not already there and the query is not over the whole table
      but over a part of it (there is a WHERE clause).
      [color=blue]
      >
      > If its the latter, you should use a WHERE clause in your SELECT and
      > perhaps also a LIMIT start,rows
      >
      > Thus,something like
      >
      > SELECT firstname,lastn ame FROM myTable WHERE ID>10 LIMIT 5,12
      >
      > will find all records that have ID>10 and return 12 records, starting
      > from the 5th one found (thus something like records with ID 15 to 27).
      >
      > If you want to read random records, request a random record (I'm
      > assuming you've got a column/cell that is AUTO_INCREMENT that you could
      > use as reference).
      >
      > Does that help you any?
      > randelld[/color]


      Comment

      • d@t@

        #4
        Re: Random select

        try
        select * from table order by rand() limit 0,5

        This doesn't have the best performance for large recordset, but is ok
        for small tables
        --
        Jay
        Wim Roffil wrote:[color=blue]
        > "Randell D." <reply.via.news .group.only.tha nks@fiprojects. moc>[/color]
        schreef in[color=blue]
        > bericht news:LpHQd.4075 06$8l.401215@pd 7tw1no...[color=green]
        > > Wim Roffil wrote:[color=darkred]
        > > > Hi,
        > > >
        > > > When I do a select with a limit I get always the same records.[/color][/color][/color]
        Usually[color=blue]
        > this[color=green][color=darkred]
        > > > is the desidered effect.
        > > >
        > > > But now I want to do a random select. So every time I do a select[/color][/color][/color]
        I[color=blue]
        > should[color=green][color=darkred]
        > > > get a different set of results.
        > > >
        > > > What is the best way to achieve this effect?
        > > >
        > > > Thanks,
        > > > Wim
        > > >
        > > >[/color]
        > >
        > > What is your problem? Do you really want random records, or are you
        > > bothered that you get the same records everytime you perform a[/color][/color]
        select?[color=blue]
        >
        > Thank you for your reply.
        >
        > I need reaaly random records. From a big collection I want to show[/color]
        the[color=blue]
        > visitors of my webpage just 5 or 10. Because adjacent records may be[/color]
        related[color=blue]
        > they should be spread over the whole collection. AUTO_INCREMENT is[/color]
        not an[color=blue]
        > option: it is not already there and the query is not over the whole[/color]
        table[color=blue]
        > but over a part of it (there is a WHERE clause).
        >[color=green]
        > >
        > > If its the latter, you should use a WHERE clause in your SELECT and
        > > perhaps also a LIMIT start,rows
        > >
        > > Thus,something like
        > >
        > > SELECT firstname,lastn ame FROM myTable WHERE ID>10 LIMIT 5,12
        > >
        > > will find all records that have ID>10 and return 12 records,[/color][/color]
        starting[color=blue][color=green]
        > > from the 5th one found (thus something like records with ID 15 to[/color][/color]
        27).[color=blue][color=green]
        > >
        > > If you want to read random records, request a random record (I'm
        > > assuming you've got a column/cell that is AUTO_INCREMENT that you[/color][/color]
        could[color=blue][color=green]
        > > use as reference).
        > >
        > > Does that help you any?
        > > randelld[/color][/color]

        Comment

        Working...