Zend数据库格式的子查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zend数据库格式的子查询相关的知识,希望对你有一定的参考价值。

我在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有错误。

答案

如果我们讨论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');

以上是关于Zend数据库格式的子查询的主要内容,如果未能解决你的问题,请参考以下文章

Zend_Db_Table 选择缺失数据,直接PHP查询ok

Mysql查询格式从ZEND更改为Yii2

检索 Zend 框架中的子表单元素?

Zend Validate - 你如何从不同的子表单传递上下文值?

Zend Framework 调用表单作为表单中的子表单

从zend 2中的mysql查询中获取所有数据