Search code examples
zend-db

Zend-Db 2.9 - SQL with RAND order


guys, I need help, I am using zend-db 2.9 - and I have an sql that need the rand() order, but the result of this query came wrong my code:

   $sql    = new Sql($this->dbAdapter);
   $select = $sql->select(
       ['a'=>$this->table]
   );

   $select->order("RAND() ASC");
   $select->limit(1);

   $stt = $sql->prepareStatementForSqlObject($select);
   $res = $stt->execute();

result is :

SELECT `a`.* FROM `mytable` AS `a` ORDER BY `RAND``(``)` ASC LIMIT 1

how to fix it?


Solution

  • I found the answer fot this: Just Add new Expression("RAND()")

        $sql = new Sql($this->dbAdapter);
        $select = $sql->select(
            ['a'=>$this->table]
        );
    
        $select->order([new Expression("RAND() ASC")]);
        $select->limit(1);
    
        $stt = $sql->prepareStatementForSqlObject($select);
        $res = $stt->execute();