Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query group shuffle [closed]

Tags:

sql

mysql

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...

like image 689
Olivier Zoletti Avatar asked Feb 01 '26 10:02

Olivier Zoletti


2 Answers

Use the rand function in ORDER BY like this:

select id, item, producer from table order by rand();
like image 149
Code Lღver Avatar answered Feb 03 '26 05:02

Code Lღver


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?

like image 24
Lt_Shade Avatar answered Feb 03 '26 04:02

Lt_Shade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!