5

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
        ";
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Kadar Annamaria
  • 831
  • 1
  • 6
  • 19
  • This is not the way to ask the question, this is some one else is solving you problem and you are putting zero effort on it.. You can ask like how inner query works in zend and how to write conditions in zend select query – Sahal Nov 12 '12 at 09:26
  • 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-table – Sahal Nov 12 '12 at 09:28
  • Exact duplicate of http://stackoverflow.com/questions/5792229/zend-framework-zend-db-select-how-to-join-custom-subquery-table – Sahal Nov 12 '12 at 09:30
  • It's not a duplicate, this question is more than just a simple JOIN – tsergium Nov 12 '12 at 09:47
  • this appears to be a useful reference incl joins http://framework.zend.com/manual/1.12/en/zend.db.select.html – Drew Nov 12 '12 at 09:29
  • "How to rewrite normal sql select with IF to Zend Framework select?" was the question, and I gave an example how I want to use it. I think this is the way you use Stackoverflow: ask a question and give an example how you want to use it, and for what. – Kadar Annamaria Jan 07 '13 at 06:44
  • @Sahal Personally I like this question, it really asks a lot of detailed questions on syntax of non-basic sql features – Ray May 13 '14 at 19:15
  • @Ray I asked this in 2012. Now I feel shame why should I asked this !! But I should not forget from where I started – Sahal May 30 '14 at 12:18

1 Answers1

2

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);
  }
tsergium
  • 1,176
  • 1
  • 14
  • 26