CakePHP 2.3中字段和位置中的子查询

CakePHP 2.3中字段和位置中的子查询,php,cakephp,Php,Cakephp,我怎么能用CakePHP点画呢? 我尝试了我知道的一切,但都不起作用: select card_type ,sum (case when used = 'Y' then (select amount from auth a1 where a1.origid = a.pnref and trxtype = 'D') else amount end) as total from auth a whe

我怎么能用CakePHP点画呢? 我尝试了我知道的一切,但都不起作用:

        select
            card_type
            ,sum (case when used = 'Y' then (select amount from auth a1 where a1.origid = a.pnref and trxtype = 'D') else amount end) as total
        from    auth a
        where
            add_date between '$this->date1' and '$this->date2'
            and     (trxtype = 'S' or trxtype = 'F')
            and     user_num = $this->user_num
            and     pnref not in (select    origid
                                    from        auth a
                                    where       add_date between '$this->date1' and '$this->date2'
                                    and trxtype = 'V')
        group by card_type
        order by 1
  • 字段中的子查询
  • 子查询在何处

[我尝试一下]

    $conditionsSubQuery['"Auth2"."add_date BETWEEN ? AND ?"'] = array($inicial_date, $final_date);
    $conditionsSubQuery['"Auth2"."trxtype"'] = 'V';

    $db = $this->User->getDataSource();
    $subQuery = $db->buildStatement(
        array(
            'fields'     => array('"Auth2"."origid"'),
            'table'      => 'auth',
            'alias'      => 'Auth2',
            'limit'      => null,
            'offset'     => null,
            'joins'      => array(),
            'conditions' => $conditionsSubQuery,
            'order'      => null,
            'group'      => null
            ),
        $this->Auth
    );

    $subQuery = ' "Auth"."pnref" NOT IN (' . $subQuery . ') ';
    $subQueryExpression = $db->expression($subQuery);

    $conditions[] = $subQueryExpression;
    $conditions[] = array(
        'Auth.date BETWEEN ? and ?' => array($inicial_date, $final_date),
        'OR' => array(
            'Auth.trxtype' => 'S',
            'Auth.trxtype' => 'F'
        ),
        'Auth.user_num' => $user_num
    );

    $fields = array(
        'Auth.card_type',
        "sum (case when Auth.used = 'Y' then (select Auth2.amount from auth Auth2 where Auth2.origid = Auth.pnref and Auth.trxtype = 'D') else Auth.amount end) as Auth.total"
    );

    $group = array('Auth.card_type');
    $order = array(1);

    return $this->Auth->find('all', compact('fields', 'conditions', 'group', 'order'));

[错误]

Database Error
Error: SQLSTATE[42601]: Syntax error: 7 ERRO: erro de sintaxe em ou próximo a "." LINE 1: ... Auth.trxtype = 'D') else Auth.amount end) as Auth.total, "U... ^

SQL Query: SELECT "Auth"."card_type" AS "Auth__card_type", sum (case when Auth.used = 'Y' then (select Auth2.amount from auth Auth2 where Auth2.origid = Auth.pnref and Auth.trxtype = 'D') else Auth.amount end) as Auth.total, "User"."user_num" AS "User__user_num" FROM "public"."system_users" AS "User" WHERE "Auth"."pnref" NOT IN (SELECT "Auth2"."origid" FROM auth AS "Auth2" WHERE "Auth2"."add_date BETWEEN '2013/02/19 06:00:00' AND '2013/02/22 10:34:17'" AND "Auth2"."trxtype" = 'V' ) AND (("Auth"."date" BETWEEN '2013/02/19 06:00:00' and '2013/02/22 10:34:17') AND ("Auth"."trxtype" = 'F') AND ("Auth"."user_num" = 68)) GROUP BY "Auth"."card_type" ORDER BY "1" ASC

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp


无论如何,谢谢。

就像@christraveres评论一样,这只是一个语法错误


查询中的问题很明显,标识符是 没有正确的联系。错误的相关条款 邮件的查询为“Auth2”。“添加日期介于'2013/02/19 06:00:00'和' “2013/02/22 10:34:17”,我怀疑您是否有一个名为 “添加日期在'2013/02/19 06:00:00'和'2013/02/22 10:34:17'之间”so 问题真的应该局限于CakePHP语法

问题解决了!
谢谢大家。

显示您尝试过的代码。至少对于子查询(选择origid…),我会保持简单,并使用一个单独的查询,然后使用该查询的结果作为第二个查询的筛选器/条件查询中的问题很明显,标识符关联不正确。错误消息查询的相关子句是
“Auth2”。“在'2013/02/19 06:00:00'和'2013/02/22 10:34:17'之间添加日期”
,我怀疑您是否有一个名为
“在'2013/02/19 06:00:00'和'2013/02/22 10:34:17'之间添加日期”的布尔列。
所以问题真的应该限于CakePHP语法。@ChrisTravers谢谢,你说得对(已经过了多少时间,所以,现在我用$this->query()方法运行原始查询)。