2

我知道这听起来非常简单,但在谷歌上根本找不到关于这个主题的文档。

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

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

我应该怎么做才能只选择这两列而不是整个表?

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

4

3 回答 3

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

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

于 2010-08-23T08:56:37.827 回答
0
$select = $db->select()
             ->from(array('t' => 'table'),
                    array('column1', 'column2'));
$stmt = $db->query($select);
$result = $stmt->fetchAll();
于 2010-08-23T08:58:14.823 回答
0
$select = $db->select()
             ->from('products',
                    array('product_id', 'product_name', 'price'));

您必须将所需的字段作为第二个参数传递给 from() 方法,第一个是表。我知道这有点令人困惑,因为在常规 sql 语法中,所需的字段首先出现,但是如果您想以模块化方式对查询进行 custruct,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')
于 2010-08-23T09:00:27.780 回答