如何编写蛋糕中的复杂子查询,即子查询在from子句中

时间:2013-09-11 09:48:05

标签: cakephp-2.0

SELECT AM.m, 
COUNT(JRC.id) FROM 
(SELECT 1 AS m 
   UNION ALL SELECT 2
   UNION ALL SELECT 3 
   UNION ALL SELECT 4 
   UNION ALL SELECT 5 
   UNION ALL SELECT 6
   UNION ALL SELECT 7
   UNION ALL SELECT 8
   UNION ALL SELECT 9
   UNION ALL SELECT 10
   UNION ALL SELECT 11
   UNION ALL SELECT 12
) AS AM
LEFT JOIN `candidates` AS `JRC`
ON   AM.m = MONTH(JRC.created) AND (JRC.jr_id = 58)
GROUP BY AM.m

这将导致12个月的月度用户数

1 个答案:

答案 0 :(得分:0)

取自这个答案:
CakePHP 2.1.x - Run a query without any models in AppController

$db = ConnectionManager::getDataSource('default');
$db->rawQuery($some_sql);

(另外,我建议你将其称为“派生表”而不是子查询。它可能看起来像一个微不足道的评论,但它可能会帮助你找到更合适的答案。)