-2

I want to pick a random row after doing an order by.

My result can sometime return 2 rows with same value. So what i want to do it that i want to select 1 row randomtly between the same rows returned.

In the below example i want to return 1 row between the 2 same rows returned.

Thanks.

select nondox, account, id 
    from rates2 where weight='32' and country_code='US' 
         and service like '%INT%' 
    order by nondox,account

Result of the above query is

nondox  account   id 
276.16  610661731 25805209
276.16  610798714 2108989
391.68  610662766 1281799

3 Answers3

0

Can provide more details ?
This will eliminate the similar nondox values it's not completely random but it should work. select account, id , nondox from rates2 where weight='32' and country_code='US' and service like '%INT%' group by nondox order by nondox,account

anouar es-sayid
  • 355
  • 5
  • 11
0

you can use function RAND() that generates a random value for each row in the table

SELECT * FROM table_name
ORDER BY RAND()
LIMIT 1;

for more informations checkout this link

Astronaute
  • 219
  • 3
  • 19
-2

you can put the result in an array, then create a random integer with random(with limit) the output the values of the index from random. example random outputs 2, you return array[random+1] ie array[3]

wanjaswilly
  • 314
  • 2
  • 11