Limit result to random

Asked

Viewed 219 times

1

I use the query

SELECT * FROM mpbbolao WHERE eTIME1>=2 LIMIT 1

for example. To receive a query result I use LIMIT 1. Is there any way I can randomize what that "1" result will be?

2 answers

2


You can use a random ordering, so you will have your first result also random.

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 has been removed of this response in the OS.

  • Had already settled, but thanks for complimenting! will definitely help others!

1

Hello, I managed to solve, follow the solution:

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

"order by Rand()" makes the order random, so the first result will also be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.