rafaelphp
rafaelphp

Reputation: 279

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?

Upvotes: 1

Views: 47

Answers (1)

rafaelphp
rafaelphp

Reputation: 279

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();

Upvotes: 1

Related Questions