0

I would like to know if it is possible to retrive one single random row from search results. I mean I have query like this:

SELECT mp.name,mp.icon,mp.id,mp.wspx,mp.wspy,ms.icon FROM maps_points as mp JOIN maps_section as ms ON(ms.id = mp.section)

I would like to get one random row from results generated from this query. Is that possible with one mysql query or should I just get all results and get this one random in PHP?

Maksym
  • 3,276
  • 3
  • 22
  • 27

3 Answers3

2

a simple way is to add ORDER BY RAND() LIMIT 1 to the query. Do take a look at some of the reasons why this can be a bad idea though, e.g.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
0
 SELECT mp.name,mp.icon,mp.id,mp.wspx,mp.wspy,ms.icon 
 FROM maps_points as mp 
 JOIN maps_section as ms ON(ms.id = mp.section) 
 ORDER BY RAND() LIMIT 1
karmafunk
  • 1,453
  • 12
  • 20
-1

Ok, I knew this one, but is there better and faster way to do that ? I mean I've seen some other approaches but they were all about random row from table and I need to pick random row from some set of results :)

Maksym
  • 3,276
  • 3
  • 22
  • 27
  • Welcome to stack overflow - please note that this isn't a forum, and you should only post answers which are actually answers. Comment on individual answers instead. – Paul Dixon Jun 25 '13 at 14:01