1

I want to retrieve random rows from table but this rows must be order in category.

select category, 
   (select order_number 
   from orders 
   where order_number in (123,125,128,129,256,263,966,258,264,159,786) 
   order by rand()) 
from orders 
order by category

This is the query I tried. But that retrieves whole data in table.


Worked query ;

SELECT category,order_number FROM (
    SELECT category,order_number 
    from orders 
    where order_number in (`$order_numbers_variable`) 
    order by rand()
) order by category
2
  • How many rows your want among all those numbers? Commented Jan 14, 2013 at 10:10
  • It's about 111 row. I mean there are 111 rows. Order_number field is unique. Category field is repeated. So I want to order by category in the same time select random order_number. That is like order in grouped field. Commented Jan 14, 2013 at 11:14

1 Answer 1

1

I assume the requirement is: Retrieve 'N' random rows from a table sorted by 'category'.

Lets assume N is 10. If you want to change the number of rows, then change it in the LIMIT clause.

SELECT * FROM (
    SELECT category from orders ORDER BY rand() ASC  LIMIT 10
) AS innerResult 
ORDER BY innerResult.category
Sign up to request clarification or add additional context in comments.

2 Comments

@Yasin Yörük Please let know if this answer helps.
Thanks friend. Your query is help me to solve my problem. I changed something little in the query.

Your Answer

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.