2013-02-22 89 views
0

如何在CakePHP中將它點出來? 我嘗試我什麼都知道,但不工作:CakePHP 2.3中字段和子查詢中的子查詢

 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')); 

[ERROR]

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 

無論如何,感謝。

+0

顯示您嘗試過的代碼。 – Rikesh 2013-02-22 12:55:46

+0

至少對於子查詢(select origid ...)我會保持簡單併爲此使用單獨的查詢,然後將結果用作第二個查詢的過濾器/條件 – thaJeztah 2013-02-22 13:01:36

+1

查詢中的問題非常明顯,該標識符被錯誤地關聯。錯誤消息的查詢的相關條款是「Auth2」。「add_date BETWEEN'2013/02/19 06:00:00'AND'2013/02/22 10:34:17'」'我懷疑你有一個布爾列名爲''add_date BETWEEN'2013/02/19 06:00:00'AND'2013/02/22 10:34:17'「'所以這個問題應該僅限於CakePHP語法。 – 2013-04-30 05:40:23

回答

1

@Chris Traveres評論,這只是一個語法錯誤。

查詢中的問題很明顯,標識符 被錯誤關聯。錯誤 消息的查詢的相關條款是「Auth2」。「add_date BETWEEN'2013/02/19 06:00:00'和 '2013/02/22 10:34:17'」,我懷疑你有一個命名布爾列 「add_date BETWEEN‘2013年2月19日06:00:00’與‘2013年2月22日10點34分十七秒’」,所以 真正的問題應該僅限於CakePHP的語法

問題解決了! 謝謝你們。