[thelist] SQL select help

rudy r937 at interlog.com
Thu Jul 26 18:21:25 CDT 2001


>most DBs have a random function that you can use

true, although you have to be careful what column you apply it to -- the
primary key will be unique, as will any other column defined as unique, but
even if you use a unique column, you might hit a gap (due to deletions) so
you would then want to take the next one after the gap, like this --

  select * from yourTable
    where yourTable.ID =
      ( select min(primaryID) from yourTable
         where primaryID >=  RANDOMFUNCTION(primaryID) )

another strategy is to sort on something silly (like the nth character of
the street address) and use TOP or LIMIT to take the first one that comes
up

also, it may depend on what you want it for -- if it's supposed to be
different every time, like a random quote, you really want to concentrate
on making it random, whereas if you only wanted a representative one, a
"sample" row might be better


rudy





More information about the thelist mailing list