如何向 Zend Table Select 添加复杂的 where 子句?

发布于 2024-08-29 02:09:50 字数 309 浏览 8 评论 0原文

我搜索了网络,但找不到任何可以向我展示良好可靠示例的内容。我的问题基本上是这样的:

如何转换:

SELECT * FROM table WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND d = 5;

Zend 语法与此类似:

$this -> 选择() ->from($this->_schema.'.'.$this->_name) ->where('a = ?', '1');

那么如何才能做到呢?

预先非常感谢。

I searched the Web and could not find anything that would show me a good solid example. My question is basically this:

How do I convert this:

SELECT * FROM table WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND d = 5;

To Zend syntax similar to this:

$this
->select()
->from($this->_schema.'.'.$this->_name)
->where('a = ?', '1');

So how can it be done?

Thank a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑寡妇 2024-09-05 02:09:50

我有类似的问题。请参阅此处答案中的代码示例:使用 Zend_Db_Table_Abstract 对 WHERE 子句进行分组

所以你最终会得到类似的结果:

$db = $this->getAdapter();
$this->select()
     ->where('(' . $db->quoteInto('a = ?', 1) . ' AND ' . $db->quoteInto('b = ?', 2) . ') OR (' . $db->quoteInto('c = ?', 3) . ' OR ' . $db->quoteInto('c = ?', 4) . ')')
     ->where('d = ?', 5);

这会给你:

SELECT `table_name`.* FROM `table_name` WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND (d = 5)

I had a similar problem. See the code example in the answer here: Grouping WHERE clauses with Zend_Db_Table_Abstract

So you would end up with something like:

$db = $this->getAdapter();
$this->select()
     ->where('(' . $db->quoteInto('a = ?', 1) . ' AND ' . $db->quoteInto('b = ?', 2) . ') OR (' . $db->quoteInto('c = ?', 3) . ' OR ' . $db->quoteInto('c = ?', 4) . ')')
     ->where('d = ?', 5);

Which would give you:

SELECT `table_name`.* FROM `table_name` WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND (d = 5)
桃酥萝莉 2024-09-05 02:09:50

1)为所有组建立条件Where/orWhere:

$conditions = $this->select()
        ->where('a= ?', 5)
        ->orWhere('b= ?', 6)
        ->getPart(Zend_Db_Select::WHERE);
// result: $conditions = "(a= 5) OR (b= 6)";

使用getPart()方法获取where条件。

2) 接下来,重置当前选择对象的 where 部分:

$this->select()->reset(Zend_Db_Select::WHERE);

3) 最后,根据需要使用 where 条件:

$this->select()
    ->where('d= ?', 5)
    ->where(implode(' ', $conditions));

http://framework.zend.com/manual/1.12/ru/zend.db.select.html

1) Build a condition for all groups Where/orWhere:

$conditions = $this->select()
        ->where('a= ?', 5)
        ->orWhere('b= ?', 6)
        ->getPart(Zend_Db_Select::WHERE);
// result: $conditions = "(a= 5) OR (b= 6)";

Use getPart() method to get the where condition.

2) Next, reset the where part of current select object:

$this->select()->reset(Zend_Db_Select::WHERE);

3) Finally, use where condition as you want:

$this->select()
    ->where('d= ?', 5)
    ->where(implode(' ', $conditions));

http://framework.zend.com/manual/1.12/ru/zend.db.select.html

方觉久 2024-09-05 02:09:50

根据 Zend Framework 网站上的 留言板帖子,这可能是不可能的。

在我看来,Zend_Db_Select 类中的 where() 和 orWhere() 不足以编写所有查询。它不支持条件嵌套,这不会在更复杂的情况下强制用户进行抽象。对于 where() 和 orWhere() 我不能这样写:

Per a message board post on the Zend Framework website, this may not be possible.

It seems to me that where() and orWhere() in the Zend_Db_Select class are not enough to be able to write all queries. It does not support the nesting of conditions, which doesn't enforce the user with abstraction in somewhat more complex cases. With where() and orWhere() I cannot write this:

给我一枪 2024-09-05 02:09:50

Edit

Zend_Db_Select->where 的数组功能仅设计用于与 IN 子句一起使用。

Example #17 Example of an array parameter in the where() method
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (product_id IN (1, 2, 3))

$productIds = array(1, 2, 3);

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

原创

<罢工>
正如 Peder 所说,你不能嵌套 orWhere,但你可以将多个参数传递给 whereorWhere

$this->select()
  ->from($this->_schema.'.'.$this->_name)
  ->where(' ( a = ? AND b = ? ) OR ( c = ? OR c = ? ) ', array(1,2,3,4))
  ->where('d = ?',array(5));

Edit

The array functionality of Zend_Db_Select->where is designed only for using it with the IN clause.

Example #17 Example of an array parameter in the where() method
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (product_id IN (1, 2, 3))

$productIds = array(1, 2, 3);

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

Original


As Peder said you can't nest orWhere but you can pass multiple arguments into where and orWhere.

$this->select()
  ->from($this->_schema.'.'.$this->_name)
  ->where(' ( a = ? AND b = ? ) OR ( c = ? OR c = ? ) ', array(1,2,3,4))
  ->where('d = ?',array(5));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文