Select random ID between 2 and 4

2

Is it possible to randomly select the ID, for example by entering ID numbers that can be drawn (2/4)?

Note: Only one line of this selection should be returned.

I put this code but it was not:

SELECT descricao
 FROM TABLE
 ORDER BY RAND ID (2,4)
 LIMIT 1;
    
asked by anonymous 18.10.2018 / 06:34

1 answer

2

You can try this:

SELECT descricao
 FROM TABLE
 WHERE ID = FLOOR(RAND() * 3 + 2)
 LIMIT 1;

The RAND() function will return a floating-point random number between 0 and 1, that is, a decimal number. To limit the result between a specific number range, which includes both the minimum number and the maximum number, you make the following account:

FLOOR( RAND() * (max - min + 1) + min )

The FLOOR() function will disregard the decimal places, returning only the integer part of a decimal number.

Source: MySQL FLOOR () function - w3resource < MySQL RAND () function - w3resource
Mysql Rand () between 2 values · GitHub

    
18.10.2018 / 10:27