Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SQLQuery() and orderby = 'RAND()'


Go to End


2582 Views

Avatar
Anatol

126 Posts

22 July 2009 at 2:00pm

Edited: 22/07/2009 2:01pm

Hi,

I want to get a random result from a quite specialised SQL query. I built the query simply using SQLQuery:

$sqlQuery = new SQLQuery();
$sqlQuery->select = array( ... etc ... );
$sqlQuery->from = array( ... etc ...);
... etc ...
$sqlQuery->orderBy = 'RAND()';

Works all perfectly except $sql->orderBy = 'RAND()';

If I order by a specific field it's fine, but ORDER BY RAND() simply gets stripped out of the query when I run

$sqlQuery->sql();

Do I do anything wrong or is RAND() simply not supported as an orderby value? If it's not supported may I suggest to include this in a future version of Silverstripe?

Cheers!
Anatol