1

I'm executing a query which I want to randomize query results from a drupal database.

here's what I have done...

$res=db_query("SELECT DISTINCT nid FROM content_type_event ORDER BY RANDOM()");

but this query doesn't work. what's wrong with this query? please help me to solve this?

Thanks a lot...

Taryn
  • 242,637
  • 56
  • 362
  • 405
Pavithra Gunasekara
  • 3,902
  • 8
  • 36
  • 46

2 Answers2

2

You probably need to be using ORDER BY RAND() instead.

Also there is a bit you may want to know about ORDER BY RAND()'s performance if you are getting a considerable amount of results that will be randomized:

http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/

EDIT: Just saw your comment that you are using PostgreSQL, this looks like it could be helpful: http://www.petefreitag.com/item/466.cfm

Sorry I can't be a lot of much help, I don't use PostgreSQL myself

Connor Smith
  • 1,274
  • 7
  • 11
1

You did not tell us which database engine you are using, but maybe ORDER BY RAND() would be better.

Update: okay, PostgreSQL uses RANDOM() and not RAND() so that's okay. I've found this question which seems to suggest that ORDER BY RANDOM() should work and the error is likely to be elsewhere. Maybe it's DISTINCT that is screwing things up; try your query again without DISTINCT.

Community
  • 1
  • 1
Tamás
  • 47,239
  • 12
  • 105
  • 124