Loading
Loading

Reputation: 19

Retrieve random data from MySQL if empty look for next

I would like to retrieve random data from a column however skip if empty. I tried everything i found here but no luck

$sql = 'SELECT video FROM data ORDER BY RAND() LIMIT 1';

I also read that Order By Rand is slow when you have huge database, if true what is the alternative

Thank you in advance

Upvotes: 0

Views: 91

Answers (1)

Digital Chris
Digital Chris

Reputation: 6202

$sql = 'SELECT video FROM data WHERE video !="" ORDER BY RAND() LIMIT 1';

Or now that I think about it, I really prefer:

$sql = 'SELECT video FROM data WHERE LENGTH(TRIM(video)) > 0 ORDER BY RAND() LIMIT 1';

Upvotes: 1

Related Questions