2

I have a query like

select id, item, producer from table

The result is like this:

  id          item            producer
   1          apple            A
   2          pear             A
   3          peach            A
   4          orange           B
   5          strawberry       B
   6          melon            B

I want to shuffle this result (order by id DESC) and get something like this

item            producer
strawberry       B
pear             A
orange           B
apple            A
peach            A
melon            B

I DON'T want to display like this:

ALL A ITEM

ALL B ITEM

ALL C ITEM...

Olivier Zoletti
  • 307
  • 1
  • 4
  • 14

3 Answers3

5

Use the rand function in ORDER BY like this:

select id, item, producer from table order by rand();
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
3

To Shuffle the selection you can use rand()

The Answer for the link below contains more information.

 SELECT id, item, producer 
 FROM table
 ORDER BY RAND()

MySQL: Shuffle a limited query result?

Community
  • 1
  • 1
Lt_Shade
  • 590
  • 1
  • 7
  • 18
2
 select id, item, producer from table order by rand()

Use Order BY rand() to randomly order the results

bear
  • 11,364
  • 26
  • 77
  • 129