如何使用Zend DB执行MySQL IN子句?

时间:2021-12-19 00:08:16

I'm trying to fetch rows that are in an array of integers that I have using Zend Framework 1.11.

我正在尝试获取使用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

当我使用$ 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 satisfying the condition where product_ref_id = 10. How can I get the IN clause to be

这只返回满足product_ref_id = 10条件的行。如何获得IN子句

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?

使用Zend DB预处理语句,以便我可以获取产品id数组中包含的所有行?

3 个解决方案

#1


15  

Don't implode the array, just pass it:

不要破坏数组,只需传递它:

->where('product_ref_id IN (?)', $product_ids);

#2


1  

It is worth mentioned that there is 2 ways to use WHERE IN clausule in Zend_Db_Select:

值得一提的是,有两种方法可以在Zend_Db_Select中使用WHERE IN clausule:

  1. We can pass array as second parameter:

    我们可以传递数组作为第二个参数:

    $select->where("column_value IN (?)", $array_of_values)

    $ select-> where(“column_value IN(?)”,$ array_of_values)

  2. Or we can simply implode array to get values as string:

    或者我们可以简单地内爆数组来获取值为字符串:

    $select->where("column_value IN (" . implode(',', $array_of_values) . ")")

    $ select-> where(“column_value IN(”。implode(',',$ array_of_values)。“)”)

#3


0  

->where('country_id IN (?)', $country_ids);

#1


15  

Don't implode the array, just pass it:

不要破坏数组,只需传递它:

->where('product_ref_id IN (?)', $product_ids);

#2


1  

It is worth mentioned that there is 2 ways to use WHERE IN clausule in Zend_Db_Select:

值得一提的是,有两种方法可以在Zend_Db_Select中使用WHERE IN clausule:

  1. We can pass array as second parameter:

    我们可以传递数组作为第二个参数:

    $select->where("column_value IN (?)", $array_of_values)

    $ select-> where(“column_value IN(?)”,$ array_of_values)

  2. Or we can simply implode array to get values as string:

    或者我们可以简单地内爆数组来获取值为字符串:

    $select->where("column_value IN (" . implode(',', $array_of_values) . ")")

    $ select-> where(“column_value IN(”。implode(',',$ array_of_values)。“)”)

#3


0  

->where('country_id IN (?)', $country_ids);