0

I have a query like this:

SELECT Id, Name, image, price, view FROM estore.product ORDER BY view DESC LIMIT 9

and I want select random 5 records in that query. I tried but this code doesn't work:

SELECT Id, Name, Image, Price, View FROM (
    SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY View DESC LIMIT 9) 
    ORDER BY RAND() LIMIT 5

How can I do? Thanks for watching?

Andomar
  • 232,371
  • 49
  • 380
  • 404
Killer Whale
  • 87
  • 2
  • 12
  • 1
    Side-note for editing: Use `four-space indentation` for your code instead of using `this` :) – Kevin Jun 17 '13 at 15:15

3 Answers3

4

A subquery must be named. Try:

LIMIT 9) as SubQueryAlias ORDER BY RAND() 
        ^^^^^^^^^^^^^^^^^^
Andomar
  • 232,371
  • 49
  • 380
  • 404
1

You may want to go and read this thread Multiple rows alternative for RAND()

If your table is quite large (and it probably can end up quite large being a product table) the rand() limit is quite a slow query

Community
  • 1
  • 1
Dave
  • 3,280
  • 2
  • 22
  • 40
0
SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY RAND() LIMIT 5

(Using subquery for order and limit the same selection is madness...)

inf3rno
  • 24,976
  • 11
  • 115
  • 197