Does anyone know how to use the order by rand? to show random records of a table. Could you show me any example?
SELECT nombre, precio FROM productos ORDER BY RAND() LIMIT 0,10
Does anyone know how to use the order by rand? to show random records of a table. Could you show me any example?
SELECT nombre, precio FROM productos ORDER BY RAND() LIMIT 0,10
According to the documentation:
This method works by generating a random value for each row of the table, sorting the table according to these random values and then returning a row.
In Other Words : This query returns randomly the data of a query
Advantage: Easy to remember and use in complex SQL queries and you do not need to have a unique assignment ID field or automatic increase in your table. Easily select multiple random rows simply by raising the LIMIT.
Disadvantage: The speed of this method is directly related to the time it takes to generate a random value for each row. The more rows a table has, the longer it will take.
Example: Returns 10 records with the name and price of the product table:
SELECT nombre, precio FROM productos ORDER BY RAND() LIMIT 10;
In this example you can notice that I ask you to show me random values of a table called products:
The quality of the consultations will depend on the amount of information that you have stored and you want to show, obviously you should consider that this method is only applied under certain criteria where the The user strictly needs to see different values every time I requested. The useful thing is that as you put in the example you use the final
LIMIT
to indicate how many records you should only consider
SELECT * FROM productos ORDER BY RAND();
So many times I execute this query, it will be the same times as the results are displayed in different ways
Now if under that same logic you want to delimit how many records show, as it appears your query would be like this:
SELECT * FROM productos ORDER BY RAND() LIMIT 3;
Once again the quality and order of the results of this consultation go to be totally dependent on the number of times the same so you can see results