我正在尝试使用 Zend Framework 2 tableGateway 生成以下查询。
SELECT
dfsstamp,
dfcstamp,
df_rstrd_flg,
dfctid,
dfname,
dfloc,
dfdesc,
dfcuser,
dfmuser,
dfid AS,
t2.dfdfid,
ctname,
cafldnme,
caid,
atid,
atvalue,
t1.record_count
FROM
(
select * from dssdocflp
where dfdfid <= 1
) as t2
LEFT JOIN
(
select dfdfid, count(*) as record_count from dssdocflp
group by dfdfid having dfdfid > 1
) as t1
on t2.dfid = t1.dfdfid
INNER JOIN dsscfgtpp ON ctid = dfctid
INNER JOIN dsscfgatp ON cactid = ctid
INNER JOIN dssattrp ON atcaid = caid AND atdfid = dfid
WHERE dfdbid = '33'
ORDER BY dfcstamp DESC, dfid ASC, caseqno ASC
以下是我到目前为止所得到的
$select = $this->tableGateway
->getSql()
->select()
->columns(array(
'dfsstamp' => new Expression('date(dfsstamp)'),
'dfcstamp' => new Expression('date(dfcstamp)'),
'df_rstrd_flg',
'dfctid',
'dfname',
'dfloc',
'dfdesc',
'dfcuser',
'dfmuser',
'dfid',
'dfdfid'
//'record_count'
))
->join('dsscfgtpp', 'ctid = dfctid', array('ctname'))
->join('dsscfgatp', 'cactid = ctid', array('cafldnme', 'caid'))
->join('dssattrp', 'atcaid = caid AND atdfid = dfid', array('atid', 'atvalue'))
->where(dfdbid = 33)
->order(array('dfcstamp DESC', 'dfid ASC', 'caseqno'))
我只需要让子查询的左连接就可以工作。我无法弄清楚我需要组合 from 和 join 方法的哪种组合。我现在已经挣扎了 3 个小时,尽管我会寻求帮助。任何帮助将不胜感激
根据@peterpeterson 的建议,这是我尝试的新查询
$subselect_t2 = new Select();
$subselect_t2->from('dssdocflp')
->where(array('dfdid <= 1'));
$subselect_t1 = new Select();
$subselect_t1->from('dssdocflp')
->columns(array(
'dfdfid',
'record_count' => new Expression('count(*)')
))
->group(array('dfdfid'))->having(array('dfdid > 1'));
$select = $this->tableGateway
->getSql()
->select()
->columns(array(
'dfcstamp',
'df_rstrd_flg',
'dfctid',
'dfname',
'dfloc',
'dfdesc',
'dfcuser',
'dfmuser',
'dfid',
'dfdfid',
't1.record_count'
))
->from(array('t2' => $subselect_t2))
->join(array('t1' => $subselect_t1), 't2.dfid = t1.dfdid', array(), 'left')
->join('dsscfgtpp', 'ctid = dfctid', array('ctname'))
->join('dsscfgatp', 'cactid = ctid', array('cafldnme', 'caid'))
->join('dssattrp', 'atcaid = caid AND atdfid = dfid', array('atid', 'atvalue'))
->where($this->documentWhereClause->create($searchData))
->order(array('dfcstamp DESC', 'dfid ASC', 'dfdfid DESC', 'caseqno'))
->limit(500);
运行我运行它我得到以下错误:
由于此对象是在构造函数中使用表和/或模式创建的,因此它是只读的。
也许我仍然没有正确地形成查询。我正在使用 tableGateway。也许不可能通过 tableGateway 进行这种查询?