This is an example from MySQL's class on Using MySQL

MySQL> Select Name
    -> From Country
    -> Where Continent="Europe"
    -> Order By RAND()
    -> LIMIT 1;

If you do Limit 3 you should get 3 random rows.

-----Original Message-----
From: Ulf Harnhammar [mailto:[EMAIL PROTECTED]] 
Sent: Wednesday, January 23, 2002 10:09 AM
To: [EMAIL PROTECTED]
Subject: Using LIMIT to select random rows

This is a suggestion for a small addition to MySQL's syntax which I'm
proposing, unless someone is kind enough to show me that there already
is
equivalent functionality somewhere else in MySQL.

See, what I want to do is select a few random rows among those that
normally would be returned. I don't want to select all matching ID's in
one select, retrieve them to my script, and then construct another
SELECT,
because this seems to be slow. I don't want to use lots of "SELECT
something FROM sometable LIMIT number,1" statements either, because this
seems to be slow too.

I propose this additional syntax to Monty and the guys:
SELECT something FROM sometable WHERE somevar=somevalue
LIMIT number1,1,number2,1,number3,1,..
which should return the rows numbered "number1", "number2", "number3"
etc
from the rows that normally would be returned. This way, I could first
use
a "SELECT COUNT(*) FROM sometable WHERE somevar=somevalue" statement,
and
then issue another statement of the kind that I propose, to get a few
random rows.

Perhaps there already is some way to do this efficiently. In that case,
please enlighten me.

Regards, Ulf Härnhammar

PS: Talking about Monty, is he by any chance the same Monty who was in a
team called Monty & Kaj and wrote a PacMan clone called Blipp for the
ABC80 computer decades ago? Man, that game was cool! ;)

________________________________________
Ulf Härnhammar
System Developer

ST-Registry
St Eriksgatan 117, E2
SE-113 43 Stockholm
SWEDEN (GMT+1)

Telephone:      +46 (0)8-545 476 04
Facsimile:      +46 (0)8-32 63 33

E-mail: [EMAIL PROTECTED]
Web: http://www.nic.st/

The STreet domain - your Internet address


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail
<[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to