dimanche 16 février 2020

MySQL select random row with each different value - comma separated input

I have a comma separated string :

input_str : 'gold,silver,gold,broze,silver'

I have one table cards which has a column color. Cards table has hundreds of rows for each of the colors.

Now, with mysql query, I want to select 1 random row from cards with these colors

So, result would be

id      color 
6251    gold
7251    silver
827127  gold
234     bronze
27816   silver

I have tried at least 10 different queries but none of them work correctly.

The approach that I used is converting the comma separated string into a temporary table and then join this temp table to the colors table and use subquery for ORDER BY RAND() but none of the queries worked for me.

Can someone kindly help please ?




Aucun commentaire:

Enregistrer un commentaire