2012-11-12 61 views
5

我有一個正常的mysql選擇,我想重寫它到一個Zend Framework的mysql選擇。這是我的選擇:如何用IF重寫正常的sql選擇到Zend Framework 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 
     "; 
+0

這不是問路這個問題,這是別人正在解決你的問題,你正在努力它..你可以問如內部查詢如何在zend中工作,以及如何在zend選擇查詢中寫入條件 – Sahal

+0

http://stackoverflow.com/questions/1340564/writing-a-subquery-using-zend-db .... http://stackoverflow.com/questions/5792229/zend-framework-zend-db-select-how-to-join-custom-subquery-表 – Sahal

+0

完全重複的 http://stackoverflow.com/questions/5792229/zend-framework-zend-db-select-how-to-join-custom-subquery-table – Sahal

回答

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); 
    } 
相關問題