0

Can someone help me with this query. I have this table for sample:

FILE Table

  • UID
  • file
  • uploaded_on_date
  • view_count

What I want to consider is the top 30 most recent uploaded file and top 30 most viewed file then randomly select from them and limit by 10.

I am new to this mysql complex queries. A sample query would be good and i will be able to understand it. Thanks.

1
  • ORDER BY RAND() type solutions will be very slow when the number of rows starts growing, just google for order by rand slow. Of course you won't notice this when selecting from 2*30 rows. Commented Feb 8, 2012 at 10:31

2 Answers 2

2
SELECT 
  *
FROM 
(
SELECT
  *
FROM
  tablefile
ORDER BY
  uploaded_on_date DESC
LIMIT 30
UNION SELECT
 *
FROM
  tablefile
ORDER BY
  view_count DESC
LIMIT 30
)
ORDER BY 
   RAND()
LIMIT 10;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply vulkanino and xdazz, really close time answered very awesome.
1
select * from (
    select * from table order by upload_on_date desc limit 30
    union
    select * from table order by view_count desc limit 30) t 
order by rand() limit 10

2 Comments

oh my, exactly the same answer!!
Thanks for the reply vulkanino and xdazz, really close time answered very awesome.

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.