sql - Trouble selecting a single random row from MySQL table -


i trying select user @ random simple table purposes of generating sample data.

the table has 2 columns, integer primary key users_id has values 1 46 inclusive, , uname varchar(60).

the query

select relusers.uname relusers relusers.users_id=floor(rand()*46+1); 

is returning multiple rows. perhaps i've been staring @ long fail see how above query ever return more 1 row. floor() returns single integer being compared primary key column. including users_id in selection shows multiple different ids being selected. 0 rows result can understand, multiple? ideas?

your code returning multiple rows because rand() evaluated on each row. so, have change of multiple matches. , chance of no matches @ all.

you can use idea, try way:

select relusers.uname relusers cross join      (selext @rand := rand()) const relusers.users_id = floor(@rand*46+1); 

this generates 1 random value , hence 1 row. but, 46 rows, order by method should perform enough:

select relusers.uname relusers order rand() limit 1; 

Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -