zend db查询获取不需要的列

时间:2011-11-04 04:07:36

标签: php mysql zend-framework

所以我有这段代码:

$db = new Zend_Db_Adapter_Pdo_Mysql($params);
$sql = $db->select()->from(array("r" => "recc"), array("r_id" => "refID"))->joinLeft(array("c" => "comment"), "r.refID = c.refID");
$results = $db->fetchAll($sql);
print_r($results);

应该转换为此查询:

SELECT refID AS r_id FROM recc r LEFT JOIN comment c ON r.refID = c.refID 

应该只返回单个列r_id,并且在使用mysql查询浏览器执行时确实返回了该列

但是当你使用db select和print_r执行结果时,除了r_id之外,它还返回了表注释中的一大堆字段,这些字段填充了空数据......

我做错了吗?如何才能按计划返回单列...

2 个答案:

答案 0 :(得分:2)

我认为这是joinLeft()。您可能需要传递一个空数组()作为第三个参数。

$sql = $db->select()
    ->from(array("r" => "recc"), array("r_id" => "refID"))
    ->joinLeft(array("c" => "comment"), "r.refID = c.refID", array());

答案 1 :(得分:0)

    Try 
    print_r($results->toArray()); //Pure array format of result
    array('')  //    If you dont want columns from a joining table you pass as the last parameter to the join    
 array('*') //IF you want all the fields