php - Select random row per distinct field value? -


i have mysql query results in this:

person | some_info  ==================    bob | pphsmbf24    bob | rz72nixdy    bob | rbqqarywk   john | kif9adxxn   john | 77tp431p4   john | hx4t0e76j   john | 4yiomqv4i   alex | n25pz8z83   alex | orq9w7c24   alex | beuz1p133    etc... 

(this simplified example. in reality there 5000 rows in results).

what need go through each person in list (bob, john, alex, etc...) , pull out row set of results. row pull out sort of random sort of based on loose set of conditions. it's not important specify conditions here i'll it's random row example.

anyways, using php, solution pretty simple. make query , 5000 rows , iterate through them pulling out random row each person. easy.

however, i'm wondering if it's possible mysql query don't have use php iterate through results , pull out random rows.

i have feeling might involve bunch of subselects, 1 each person, in case solution more time, resource , bandwidth intensive current solution.

is there clever query can accomplish in 1 command?

here sqlfiddle can play with.

to random value distinct name use

select r.name,  (select r1.some_info test r1 r.name=r1.name order rand() limit 1)     'some_info'  test r  group r.name ;   

put query stands in sqlfiddle , work

im using r , r1 table alias names. use subquery select random some_info name

sql fiddle here


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -