How do I select random rows in MySQL? -
mytable pid name field === ==== ===== 1 a1 0 2 a2 1 3 a3 1 4 a4 0 5 a5 0
this table structure. here want select randomly 4 rows use rand() mysql function in query questions
how pair rows. mean, wanna select pid 2 , 3 1 ofter another. need in bellow order. i don't want break pair a2 a3
a1 a2 a3 a4
or a2 a3 a4 a1
or a2 a3 a4 a5
or a4 a5 a2 a3
, etc
i used query below it's not working me
select * mytable order rand() asc limit 0,4
turbod close answer, ordering randomly, when seems wanted order pid, after getting random rows wanted in conjunction ones concerning a2 , a3:
( select * `mytable` name ='a2' or name ='a3' limit 2 ) union ( select distinct * `mytable` name !='a2' or name !='a3' order rand( ) limit 2 ) order `pid`
Comments
Post a Comment