English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Doos anyone know if there is such thing as a random select querie with mySQL. I want to randomly select and echo about 50 people from my database. anyone know?

2006-09-12 09:42:19 · 4 answers · asked by peter s 1 in Computers & Internet Programming & Design

4 answers

Without an ORDER BY clause, you can never guarantee the order data is returned, so a standard
SELECT * FROM table
is technically random

However, if you would like a little more randomness, try this:

SELECT * FROM table ORDER BY rand()

2006-09-12 09:47:25 · answer #1 · answered by wfinn 2 · 2 0

If you were using SQLServer you could do a sproc like such :

CREATE PROCEDURE [dbo].[spr_randomrecord]
AS

DECLARE @Rows int

SET @Rows = SELECT Max(ID) FROM Tables

get_random:
SELECT * FROM Table
WHERE ID = (SELECT CAST((RAND() * @Rows) AS int) + 1)

IF (@@rowcount = 0)
GOTO get_random
GO

But, your not.

2006-09-12 16:50:09 · answer #2 · answered by Special Ed 5 · 0 0

You just limit the random number to the number of records.

Then you say

Select * from

start $randNumber limit 1

Helmut

2006-09-12 17:23:38 · answer #3 · answered by hswes 2 · 0 1

Instead of
SELECT field
FROM table

Do:
SELECT TOP 50 field
FROM table

2006-09-12 16:47:49 · answer #4 · answered by Alex 1 · 0 1

fedest.com, questions and answers