Skip to content

Commit 9b62b34

Browse files
committed
Add RAND () orderBy value
1 parent 35bdeb5 commit 9b62b34

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

MysqliDb.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,12 @@ protected function _buildOrderBy () {
794794
return;
795795

796796
$this->_query .= " ORDER BY ";
797-
foreach ($this->_orderBy as $prop => $value)
798-
$this->_query .= $prop . " " . $value . ", ";
797+
foreach ($this->_orderBy as $prop => $value) {
798+
if (strtolower (str_replace (" ", "", $prop)) == 'rand()')
799+
$this->_query .= "rand(), ";
800+
else
801+
$this->_query .= $prop . " " . $value . ", ";
802+
}
799803

800804
$this->_query = rtrim ($this->_query, ', ') . " ";
801805
}

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ $results = $db
254254
```php
255255
$db->orderBy("id","asc");
256256
$db->orderBy("login","Desc");
257+
$db->orderBy("RAND ()");
257258
$results = $db->get('users');
258-
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC;
259+
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
259260
```
260261

261262
### Grouping method

0 commit comments

Comments
 (0)