如何使用 Zend DB 执行 MySQL IN 子句?
我正在尝试使用 Zend Framework 1.11 获取整数数组中的行。
$this->dbSelect
->from($table_prefix . 'product_link')
->joinLeft($table_prefix . 'product_link_name', $table_prefix . 'product_link.product_link_name_ref_id = ' . $table_prefix . 'product_link_name.product_link_name_id')
->where('product_ref_id IN (?)', implode(', ', $product_ids));
当我使用 $this->dbSelect
的 __toString()
方法时,我得到
SELECT `phc_distrib_product_link`.*,
`phc_distrib_product_link_name`.*
FROM `phc_distrib_product_link`
LEFT JOIN `phc_distrib_product_link_name`
ON phc_distrib_product_link.product_link_name_ref_id = phc_distrib_product_link_name.product_link_name_id
WHERE (product_ref_id IN ('10, 12'))
This only returns rows met满足product_ref_id = 10条件的行。 如何使 IN 子句成为
product_ref_id IN ('10', '12')
或
product_ref_id IN (10, 12)
使用 Zend DB 准备好的语句,以便我可以获取产品 id 数组中包含的所有行?
I'm trying to fetch rows that are in an array of integers that I have using Zend Framework 1.11.
$this->dbSelect
->from($table_prefix . 'product_link')
->joinLeft($table_prefix . 'product_link_name', $table_prefix . 'product_link.product_link_name_ref_id = ' . $table_prefix . 'product_link_name.product_link_name_id')
->where('product_ref_id IN (?)', implode(', ', $product_ids));
When I use the __toString()
method of $this->dbSelect
, I get
SELECT `phc_distrib_product_link`.*,
`phc_distrib_product_link_name`.*
FROM `phc_distrib_product_link`
LEFT JOIN `phc_distrib_product_link_name`
ON phc_distrib_product_link.product_link_name_ref_id = phc_distrib_product_link_name.product_link_name_id
WHERE (product_ref_id IN ('10, 12'))
This only returns rows satisfying the condition where product_ref_id = 10.
How can I get the IN clause to be
product_ref_id IN ('10', '12')
or
product_ref_id IN (10, 12)
using Zend DB prepared statements so I can fetch all rows contained inside the product id array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要破坏数组,只需传递它:
Don't implode the array, just pass it:
值得一提的是,在 Zend_Db_Select 中有两种使用 WHERE IN 子句的方法:
我们可以将数组作为第二个参数传递:
$select->where("column_value IN (?)", $array_of_values)
或者我们可以简单地内爆数组以获取字符串形式的值:
$select->where("column_value IN (" .implode(',', $array_of_values) . ")")
It is worth mentioned that there is 2 ways to use WHERE IN clausule in Zend_Db_Select:
We can pass array as second parameter:
$select->where("column_value IN (?)", $array_of_values)
Or we can simply implode array to get values as string:
$select->where("column_value IN (" . implode(',', $array_of_values) . ")")