Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite normal sql select with IF to Zend Framework select?

I have a normal mysql select and I would like to rewrite it to a Zend Framework mysql select. Here is my select:

$sql = "
     SELECT 
        IF(mu.recieverUserId = '{$userId}', u.senderUserId, 
            mu.recieverUserId) friend1,
            u.mesaj, u.senderUserId, mu.recieverUserId,
            u.created
        FROM 
            (SELECT * 
            FROM mesaje
            ORDER BY `created` desc) AS u
        LEFT JOIN `mesaje_utilizatori` AS `mu` ON u.id=mu.mesajId   
        WHERE (mu.recieverUserId = '{$userId}' OR   u.senderUserId='{$userId}')             
              GROUP BY friend1 ASC 
          ORDER BY `u`.`created` DESC, u.id DESC
        ";
like image 604
Kadar Annamaria Avatar asked Nov 04 '22 11:11

Kadar Annamaria


1 Answers

Here is the documentation: Zend_Db_Statement

Zend Query

$sql = $db->query(
   'SELECT 
   IF(mu.recieverUserId = ?, u.senderUserId, 
     mu.recieverUserId) friend1,
   u.mesaj, u.senderUserId, mu.recieverUserId,
   u.created
    FROM 
   (SELECT * 
   FROM mesaje
   ORDER BY `created` desc) AS u
   LEFT JOIN `mesaje_utilizatori` AS `mu` ON u.id=mu.mesajId 
    WHERE (mu.recieverUserId = ? OR u.senderUserId = ? )    
    GROUP BY friend1 ASC 
    ORDER BY `u`.`created` DESC, u.id DESC',
   array($userId, $userId,$userId)
  );

Zend Result

 while ($row = $sql->fetch()) {
   Zend_Debug::dump($row);
  }
like image 177
tsergium Avatar answered Nov 09 '22 10:11

tsergium