2016-06-07 65 views
-1

我想用在我的控制器此MySQL查詢,而無需使用查詢()。控制器分頁使用子查詢

SELECT * FROM T1 INNER JOIN 
(SELECT T1.id 
FROM T1 
INNER JOIN T2 ON T1.id = T2.T1_id 
INNER JOIN T3 ON T2.T3_id = T3.id 
WHERE 
1 = 1 
GROUP BY T1.id 
HAVING COUNT(*) = 1) AS T1_x ON T1.id = T1_x.id 

無法弄清楚我的控制器中的代碼是什麼。

回答

1

這裏是如何在CakePHP中加入一個子查詢

我的模型名稱這裏的一個方法是T1

$this->paginate = array(
    'paramType' => 'querystring', 
    'joins' => array(
     array(
     'table' => '(SELECT T1.id 
        FROM T1 
        INNER JOIN T2 ON T1.id = T2.T1_id 
        INNER JOIN T3 ON T2.T3_id = T3.id 
        WHERE 
        1 = 1 
        GROUP BY T1.id 
        HAVING COUNT(*) = 1)' 
      'alias' => 'T1_x', 
      'type' => 'INNER', 
      'conditions' => array('T1_x.id = T1.id') 
      ) 
     ) 
); 


$T1 = $this->paginate();