本文介绍了从codeigniter中的where_in删除单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用search功能.我创建了一个搜索表单,用户可以在其中基于Typeope& Formate.

I am working on a search functionality.I have created a search form in which user can search an Application based on Type,ope & Formate.

我在 join 查询中使用了子查询以得到所需的结果.我已经在MySQL Workbench中测试了我的查询,但仍然可以正常工作.

I have used a subquery in my join query to get the desired result. I have tested my query in MySQL Workbench nad it's working fine.

但是当我使用查询生成器技术在Codeigniter中尝试相同的查询时,我遇到了一个问题.

But when I tried that same query in Codeigniter using query builder technique then I am facing a problem.

这是在工作台中可以正常工作的查询:

Here is the query which is worked fine in workbench:

SELECT (*)
FROM `App`
LEFT JOIN `App_type` 
ON `App_type`.`app_id` = `App`.`id`
LEFT JOIN `App_formate` 
ON `App_formate`.`app_id` = `App`.`id`
WHERE `App`.`id` IN(select app_id FROM App_type WHERE type_id in (3,2,6) group by app_id HAVING COUNT(*) = 3)
AND `App_formate`.`formate_id` IN('1', '3')
AND `jobs`.`ope_min` <= '3'
AND `jobs`.`ope_max` >= '3'
GROUP BY `jobs`.`id`;

这是我使用的联接查询:

This is the join query which I use:

$subquery = "select app_id FROM App_type WHERE type_id in ($selected_type) group by app_id HAVING COUNT(*) = $type_count";

$search_app_query = $this->db
    ->select('*')
    ->from('App')
    ->join('App_type', 'App_type.app_id = App.id', 'left outer')
    ->join('App_formate', 'App_formate.app_id = App.id', 'left outer')      
    ->where_in('App.id',$subquery)  //<-- Here is the problem
    ->where_in('App_formate.formate_id',$data['selected_formates'])
    ->where('App.ope_min <=',$data['ope_value'])
    ->where('App.ope_max >=',$data['ope_value'])    
    ->group_by("App.id", "desc")
    ->get();

虽然我正在调试此问题,但它显示了

While I am debugging this problem it shows the

 I have found the problem is in this part of the query:
 "WHERE `App`.`id` IN('select app_id 
 FROM App_type 
 WHERE type_id in (3,2,6) 
 group by app_id HAVING COUNT(*) = 3')"

此子查询中的单引号引起了问题.

that single quote in this subquery is creating a problem.

到目前为止我尝试过的事情:

What I have tried so far:

要删除此单引号,我已经尝试过

To remove this single quote I have tried

  1. REPLACE($subquery, '''', '')
  2. ->where_in('App.id',trim($subquery,"'"))
  3. $subquery_improved = substr($subquery, 1, -1);
  1. REPLACE($subquery, '''', '')
  2. ->where_in('App.id',trim($subquery,"'"))
  3. $subquery_improved = substr($subquery, 1, -1);

但是所有这些解决方案都行不通.他们没有删除单引号.

But all this solution is not working. They are not removing the single quote.

注意:我知道$this->db->query(),但不想使用它.

Note: I am aware of $this->db->query() but do not want to use that.

推荐答案

您的任务看起来很简单

代替

->where_in('App.id',$subquery)  //<-- Here is the problem

您可以尝试以下

->where("App.id IN (".$subquery.")",NULL, false)

这篇关于从codeigniter中的where_in删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:54