1

I searched everywhere to find an SQL query to select rows randomly without changing the order. Almost everyone uses something like this:

SELECT * FROM table WHERE type = 1 ORDER BY RAND() LIMIT 25 

But above query changes the order. I need a query which selects randomly among the rows but doesn't changes the order, cause every record has a date also.

Harry
90k26 gold badges215 silver badges223 bronze badges
asked Dec 26, 2014 at 12:51
0

1 Answer 1

2

Select the random rows and then re-order them:

select t.*
from (select *
 from table t
 where type = 1
 order by rand()
 limit 25
 ) t
order by datecol;

In SQL, if you want rows in a particular order, you need to use an explicit order by clause. You should never depend on the ordering of results with no order by. SQL does not guarantee the ordering. MySQL does not guarantee the ordering, unless the query has an order by.

answered Dec 26, 2014 at 12:55

1 Comment

I want to export above query result in PHPMYADMIN but it has no sql output format but I can see sql format in other queries

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.