Zend mysql fetchAll和count

时间:2012-04-21 13:25:34

标签: php mysql zend-framework

我有以下SQL查询:

SELECT `ka`.`id`, COUNT(kk.id) AS `clicks` FROM `karriere_anzeige` AS `ka` LEFT JOIN `karriere_klicks` AS `kk` ON `ka`.`id` = `kk`.`id_anzeige` WHERE (ka.id_kunde = '616') GROUP BY `ka`.`id`

如果我在phpMyAdmin中运行此查询,我会得到正确的结果,例如:

id |点击

4803 | 75

4822 | 144

然而这次尝试:

$rowset = $db->fetchAll($select);

返回此行集:

array(2) {
    [0] => array(2) {
        ["id"] => string(4) "4803"
        ["clicks"] => string(1) "0"
    }
    [1] => array(2) {
        ["id"] => string(4) "4822"
        ["clicks"] => string(1) "0"
    }
}

$ db是Zend_Db_Adapter_Pdo_Mysql对象

当我执行INNER JOIN而不是LEFT JOIN时,在phpMyAdmin中运行SQL查询会返回几行。如上所述,对Zend Framework执行相同操作会返回零行。我想我一般都会做错事,但我无法弄清楚它是什么。有人可以给我一个暗示吗?

1 个答案:

答案 0 :(得分:0)

如果您尝试传递字符串,请尝试SQL:

//This assumes access from a controller/action
$db = Zend_Db_Table::getDefaultAdapter();
$sql = "SELECT `ka`.`id`, COUNT(kk.id) AS `clicks` FROM `karriere_anzeige` AS `ka` LEFT JOIN `karriere_klicks` AS `kk` ON `ka`.`id` = `kk`.`id_anzeige` WHERE (ka.id_kunde = '616') GROUP BY `ka`.`id`";
$stmt = new Zend_Db_Statement_PDO($db, $sql);
$stmt->execute();

您还可以构建一个Zend_Db_Select对象来查询您的表格。