非常简单的Zend_Db_Table问题

时间:2010-08-23 08:54:14

标签: php zend-db-table

我知道这听起来非常容易,但谷歌上没有关于这个主题的文档。

我想从数据库中选择两列。我创建了一个Zend_Db_Table对象并将其指向我的表。

现在我想选择两列:customerId和name。

如何选择这两列而不是整个表?

提前谢谢,我会给你烤蛋糕或打扫你的房间。

3 个答案:

答案 0 :(得分:3)

$table->fetchAll(
    $table->select()
          ->from('table', array('column1', 'column2'))
);

谢谢,我已经有了女仆;)

答案 1 :(得分:0)

$select = $db->select()
             ->from(array('t' => 'table'),
                    array('column1', 'column2'));
$stmt = $db->query($select);
$result = $stmt->fetchAll();

答案 2 :(得分:0)

$select = $db->select()
             ->from('products',
                    array('product_id', 'product_name', 'price'));

你必须将所需的fiels作为第二个参数传递给from()方法,第一个是表。 我知道它有点令人困惑,因为在常规的sql语法中,所需的字段首先出现,但如果你想以模块化的方式来构建查询,那么zend db会非常方便。 接受字符串数组和单个字符串

另一个例子:

Example #11 Examples of adding columns with the columns() method
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db->select()
             ->from(array('p' => 'products'), 'product_id')
             ->columns('product_name');

// Build the same query, specifying correlation names:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db->select()
             ->from(array('p' => 'products'), 'p.product_id')
             ->columns('product_name', 'p');
             // Alternatively use columns('p.product_name')