【问题标题】:Convert SQL satement to Zend Db_Table_Abstract_Select code将 SQL 语句转换为发送 Db_Table_Abstract_Select 代码
【发布时间】:2016-05-09 11:18:18
【问题描述】:

我正在尝试从下表中获取每个客户的最新行。

----------------------------------------------------------------------------------
|  id  |  customer_id  |  type   |  type_id  |    notes    |  timestamp   |  uid |
----------------------------------------------------------------------------------
|  1   |       1       |  sales  |     9     |  Note 1...  |  1432781613  |   9  |
|  2   |       2       |  sales  |     9     |  Note 1...  |  1432791204  |   9  |
|  3   |       3       |  sales  |     9     |  Note 1...  |  1432881619  |   9  |
|  4   |       1       |  sales  |     9     |  Note 2...  |  1442771601  |   9  |
|  5   |       1       |  sales  |     9     |  Note 3...  |  1462781617  |   9  |

我有以下有效的代码和 SQL 语句...

$type="sales";

$sql =  "
        SELECT cl1.*
        FROM {$this->_name} cl1
        INNER JOIN  (
                    SELECT customer_id, MAX(timestamp) AS lastTimestamp
                    FROM {$this->_name}
                    WHERE type = '{$type}'
                    GROUP BY customer_id
                    ) cl2
        ON cl1.customer_id = cl2.customer_id AND cl1.timestamp = cl2.lastTimestamp
        ";

$stmt = $this->getAdapter()->query($sql);

产生...

SELECT cl1.* FROM customer_contactLog cl1 INNER JOIN ( SELECT customer_id, MAX(timestamp) AS lastTimestamp FROM customer_contactLog WHERE type = 'sales' GROUP BY customer_id ) cl2 ON cl1.customer_id = cl2.customer_id AND cl1.timestamp = cl2.lastTimestamp

我试图将其转换为“Zend 方式”,因为我所有的其他模型都是这样编写的,但我很挣扎。 我想出的代码是……

    $select = $this ->select()
                    ->from      (
                                array('cl1' => $this->_name),
                                array('cl1.*')
                                )
                    ->join  (
                                array('cl2' => $this->_name),
                                "cl2.type = '{$type}'",
                                array('cl2.customer_id', 'MAX(cl2.timestamp) AS lastTimestamp')
                                )
                    ->where     ('cl1.customer_id = ?', 'cl2.customer_id')
                    ->where     ('cl1.timestamp = ?', 'cl2.lastTimestamp');

但这会产生...

    SELECT `cl1`.*, `cl2`.`customer_id`, MAX(cl2.timestamp) AS `lastTimestamp` FROM `customer_contactLog` AS `cl1` INNER JOIN `customer_contactLog` AS `cl2` ON cl2.type = 'sales' WHERE (cl1.customer_id = 'cl2.customer_id') AND (cl1.timestamp = 'cl2.lastTimestamp')

谁能告诉我哪里出错了?

谢谢

【问题讨论】:

    标签: php mysql zend-framework inner-join


    【解决方案1】:

    您可以在 Zend DB 查询中使用嵌套选择。这正是您需要的 - 创建一个子选择,然后将其加入主选择。

    $maxTimestampSelect = $this->select()
        ->from(
            $this->_name, 
            array('customer_id', 'lastTimestamp' => new Zend_Db_Expr('MAX(timestamp)'))
        )
        ->where('type = ?', $type)
        ->group('customer_id');
    
    $select = $this->select()
        ->from(
            array('cl1' => $this->_name), 
            array('cl1.*')
        )
        ->join(
            array('cl2' => $maxTimestampSelect), 
            'cl1.customer_id = cl2.customer_id AND cl1.timestamp = cl2.lastTimestamp',
            null
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2013-02-15
      • 2019-07-03
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多