Zend数据库格式的子查询

问题描述 投票:0回答:1

我在MYSQL中创建了一个查询,如下所示:

select subsql.item_id, subsql.uid, subsql.post_date, subsql.uid_prev,
max(workflow_trans.date) AS pre_date

from (
    select item_id, max(date) AS post_date, uid, uid_prev
    from workflow_trans 
    where uid = 'name' and date <= 'date1' and date >= 'date2'
    group by item_id, uid, uid_prev
) AS subsql

left join workflow_trans on subsql.item_id = workflow_trans.item_id and
subsql.uid_prev = workflow_trans.uid and subsql.post_date > workflow_trans.date

where workflow_trans.date is not null
group by subsql.item_id, subsql.uid, subsql.post_date, subsql.uid_prev
order by subsql.post_date

In将其翻译成Zend DB格式如下:

$this->db->select()->from(array("subsql" => $this->db->select()
->from(array($this->_name), array('item_id', 'max(date) AS post_date', 
'uid', 'uid_prev'))
->where("uid = ?", $username)->where("date >= ?", $tgl1)
->where("date <= ?", $tgl2)
->group(array('item_id', 'uid', 'uid_prev'))), 
array('subsql.item_id','subsql.uid',
'subsql.post_date','subsql.uid_prev','max(workflow_trans.date)
as pre_date'))
->joinLeft($this->_name, 'subsql.item_id = workflow_trans.item_id 
and subsql.uid_prev = workflow_trans.uid and subsql.post_date > 
workflow_trans.date')
->where("workflow_trans.date != ?", null)
->group(array('subsql.item_id', 'subsql.uid', 'subsql.post_date',
'subsql.uid_prev'))
->order(array('subsql.post_date'))

然而,Zend模型无法运作。我已经对它进行了重新检查,并进行了3次更改,但格式仍有问题。新的一双锐利的眼睛是赞赏的。

答案更新

大家好,我已经找到它,在运行代码下面:

$this->db->select()->from(array("subsql" => $this->db->select()
->from(array($this->_name), array('item_id', 'max(date) AS post_date', 
'uid', 'uid_prev'))
->where("uid = ?", $username)->where("date >= ?", $tgl1)
->where("date <= ?", $tgl2)
->group(array('item_id', 'uid', 'uid_prev'))), 
array('subsql.item_id','subsql.uid','subsql.post_date','subsql.uid_prev'))
->joinLeft(array($this->_name), 'subsql.item_id = workflow_trans.item_id and
subsql.uid_prev = workflow_trans.uid and
subsql.post_date > workflow_trans.date', 
array('max(workflow_trans.date) as pre_date'))
->where("workflow_trans.date is not null")
->group(array('subsql.item_id', 'subsql.uid', 'subsql.post_date',
'subsql.uid_prev'))
->order(array('subsql.post_date'));

你应该在自己的join函数中定义连接表中的列,当然我对如何处理null有错误。

mysql zend-framework subquery zend-db
1个回答
1
投票

如果我们讨论zf3,这个查询会生成与你的相同的sql。

$from = $this->getSql()->select('workflow_trans')
                ->columns(['item_id, max(date) AS post_date, uid, uid_prev', new Literal('max(workflow_trans.date) AS pre_date')])
                ->where(['uid = ?' => 'name'])
                ->where(['date <= ?' => 'date1'])
                ->where(['date >= ?' => 'date2'])
                ->group(['item_id', 'uid', 'uid_prev']);

$query = $this->getSql()->select()
                    ->columns(['item_id, uid, post_date, uid_prev'])
                    ->from(['subsql' => $from])
                    ->join('workflow_trans', 'subsql.item_id = workflow_trans.item_id and subsql.uid_prev = workflow_trans.uid and subsql.post_date > workflow_trans.date', [])
                    ->where(new IsNotNull('workflow_trans.date'))
                    ->group(['subsql.item_id', 'subsql.uid', 'subsql.post_date', 'subsql.uid_prev'])
                    ->order('subsql.post_date');
© www.soinside.com 2019 - 2024. All rights reserved.