CsharpBeginner
CsharpBeginner

Reputation: 1773

Random Items from Query

I have a database with 16000 rows. I want to grab a random 400 rows.

How would I accomplish this task? Would I do it in Sql? Or select all 16000 rows and then dump a random 400 into an array?

I'm new to PHP and programming..

Thanks for any help.

$result = mysql_query ('SELECT * FROM AllImages') or die ('Error query: '.mysql_error ());

Upvotes: 1

Views: 72

Answers (3)

pQd
pQd

Reputation: 126

you can try this:

$result = mysql_query ('SELECT * FROM AllImages ORDER BY RAND() LIMIT 400');

although it's not very well performing solution if you have a lot of rows

Upvotes: 1

Waleed Khan
Waleed Khan

Reputation: 11477

SELECT ... ORDER BY RAND() LIMIT 400

Also, mysql_* is deprecated.

Upvotes: 3

jcho360
jcho360

Reputation: 3759

SELECT * FROM AllImages order by RAND() limit 400

Upvotes: 3

Related Questions