Limit result to a random

1

I use the query

SELECT * FROM mpbbolao WHERE eTIME1>=2 LIMIT 1

for example. To get a query result I use LIMIT 1 . How can I randomize what this "1" result will be?

    
asked by anonymous 21.10.2017 / 20:38

2 answers

2

You can use a random sort order, so you get your first random result as well.

MySQL

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

MySQL Reference Manual: Mathematical Functions (RAND ())

PostgreSQL

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

PostgreSQL Documentation: Mathematical Functions and Operators

Microsoft SQL Server

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Microsoft Docs: NEWID () (Transact-SQL)

IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

IBM Knowledge Center: RAND

Oracle

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Oracle Docs: DBMS_RANDOM

The code was taken from this answer in SO .

    
21.10.2017 / 20:43
1

Hello, I was able to solve it, here is the solution:

SELECT * FROM mpbbolao WHERE eTIME1>=2 order by rand() LIMIT 1

"order by rand ()" causes the order to be random, so the first result will also be.

    
21.10.2017 / 20:41