我尝试从此查询中获取结果
$sql = " SET @col = NULL; SET @sql = NULL; Select Group_Concat(Distinct Concat( 'SUM(CASE WHEN tbl.sdate = ''',colname,''' THEN tbl.result ELSE NULL END) AS ''','''' ) ) Into @col From ( select concat(month(i.invdate),'.',year(i.invdate)) as colname from invoices as i where i.invtype = 1 and i.pid = 5 order by i.invdate ) As collst; SET @sql = CONCAT('SELECT tbl.wrkname,',@col,' FROM ( Select wl.wgname As wrkname,Concat(Month(i.invdate),''.'',Year(i.invdate)) as sdate,Sum(id.qty * id.price) As result From invoices As i Join invoicedetails As id On i.pchid = id.pchid Join workgroups As w On i.wid = w.wid Join workgrouplist As wl On w.wglid = wl.wglid Where i.invtype = ',1,' And i.pid =',5,' Group By i.pid,sdate Order By i.invdate,wrkname ) AS tbl GROUP BY tbl.wrkname'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; "
Phalcon版本:1.2.4
Profiler日志是:
sql Statement: SET @col = NULL; SET @sql = NULL; Select Group_Concat(Distinct Concat( 'SUM(CASE WHEN tbl.sdate = ''','''' ) ) Into @col From ( select concat(month(i.invdate),year(i.invdate)) as colname from invoices as i where i.invtype = 1 and i.pid = 5 order by i.invdate ) As collst; SET @sql = CONCAT('SELECT tbl.wrkname,' FROM ( Select wl.wgname As wrkname,Sum(id.qty * id.price) As result From invoices As i Join invoicedetails As id On i.pchid = id.pchid Join workgroups As w On i.wid = w.wid Join workgrouplist As wl On w.wglid = wl.wglid Where i.invtype = ',sdate Order By i.invdate,wrkname ) AS tbl GROUP BY tbl.wrkname'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; Start Time: 1388927869.2788 Final Time: 1388927869.2808 Total Elapsed Time: 0.0019619464874268
问题解决了:我创建了存储过程并插入了所有查询.
接下来我在PHP脚本中调用存储过程.
$sql = "CALL GetReportByProjectBetweenDates(1,5)"; $result = $this->db->query($sql);
解决方法
我只看到你的sql,你想展示你是如何尝试在phalcon中运行它的吗?
在不使用模型或元数据的情况下运行原始sql的一种简单方法是使用基本适配器,在本例中我将使用PDO:
$connection = new Phalcon\Db\Adapter\Pdo\MysqL(array( 'host' => 'localhost','username' => 'user','password' => 'password','dbname' => 'optional' )); $connection->connect(); $sql = 'select * from database.table'; $result_set = $connection->query($sql); $result_set->setFetchMode(Phalcon\Db::FETCH_ASSOC); $result_set = $result_set->fetchAll($result_set);