5

我有一个普通的 mysql select,我想将它重写为 Zend Framework mysql 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
        ";
4

1 回答 1

2

这是文档:Zend_Db_Statement

Zend 查询

$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 结果

 while ($row = $sql->fetch()) {
   Zend_Debug::dump($row);
  }
于 2012-11-12T09:45:50.413 回答