php - FIND_IN_SET 在 codeigniter 的查询中自动添加 IS NULL 并且还需要在我的查询中添加括号以进行分组

标签 php mysql sql codeigniter filter

这就是我在查询中使用 FIND_IN_SET 的方式,它在查询中自动添加 IS NULL 并且还需要在 WHERE s.cat_id = '11 之后添加括号' 和查询

$spilt=explode(',',$newstyle);

foreach ($spilt as $splitkey => $splitvalue) {
    if ($splitkey == 0) {
        $this->db->where("FIND_IN_SET(".$splitvalue.",w_card_style)");
    }
    else{
        $this->db->or_where("FIND_IN_SET(".$splitvalue.",w_card_style)");
    }
}

当我使用 $this->db->last_query() 打印查询时,它给出类似的查询

SELECT `c`.`city_name`, `s`.`name`, `s`.`location`, `s`.`starting_price`, `s`.`budget_range`, `s`.`cat_id`, `s`.`id`, `i`.`path`, `s`.`specification_status`, `s`.`per_unit_charge`, `s`.`w_card_style`, `s`.`w_card_style`, `s`.`budget_range` FROM `image_gallery` as `i` JOIN `specification` as `s` ON `i`.`ser_id`=`s`.`id` JOIN `cities` as `c` ON `c`.`city_id`=`s`.`location` JOIN `wcard_style` as `ws` ON `ws`.`id`=`s`.`w_card_style` WHERE `s`.`cat_id` = '11' AND FIND_IN_SET(1,w_card_style) IS NULL OR FIND_IN_SET(2,w_card_style) IS NULL OR FIND_IN_SET(3,w_card_style) IS NULL AND budget_range BETWEEN 50 AND 100 GROUP BY `ser_id` HAVING `cat_id` = '11' ORDER BY `s`.`budget_range` DESC

在每个 FIND_IN_SET 之后都有 IS NULL,那么如何从 Query 中删除 IS NULL 并且还需要在 "WHERE s.cat_id = '11' 之后添加括号AND "在运行时这样

SELECT `c`.`city_name`, `s`.`name`, `s`.`location`, `s`.`starting_price`, `s`.`budget_range`, `s`.`cat_id`, `s`.`id`, `i`.`path`, `s`.`specification_status`, `s`.`per_unit_charge`, `s`.`w_card_style`, `s`.`w_card_style`, `s`.`budget_range` FROM `image_gallery` as `i` JOIN `specification` as `s` ON `i`.`ser_id`=`s`.`id` JOIN `cities` as `c` ON `c`.`city_id`=`s`.`location` JOIN `wcard_style` as `ws` ON `ws`.`id`=`s`.`w_card_style` WHERE `s`.`cat_id` = '11' AND (FIND_IN_SET(1,w_card_style) IS NULL OR FIND_IN_SET(2,w_card_style) IS NULL OR FIND_IN_SET(3,w_card_style) IS NULL ) AND budget_range BETWEEN 50 AND 100 GROUP BY `ser_id` HAVING `cat_id` = '11' ORDER BY `s`.`budget_range` DESC`enter code here`

最佳答案

试试这个代码我已经修改了你的代码,做了一些改变。使用我下面的代码它的工作。

$spilt=explode(',',$newstyle);
          foreach ($spilt as $splitkey => $splitvalue) {
            if ($splitkey == 0) {
            $this->db->where("FIND_IN_SET(".$splitvalue.",w_card_style)!=",0);
            }
            else{
              $this->db->or_where("FIND_IN_SET(".$splitvalue.",w_card_style)!=",0);
            }
          }

关于php - FIND_IN_SET 在 codeigniter 的查询中自动添加 IS NULL 并且还需要在我的查询中添加括号以进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842734/

相关文章:

php - MySQL -> 选择第二低的值

计数超过 1 的 group by 的 mySQL 语句

mysql - 标题中特定日期的正则表达式

php - Laravel 明显没有做好它的工作

php - 使用单例而不是新运算符检索对象时 netbeans 自动完成?

php - 操作一个 3000 万字符长的字符串

php/Ajax - 预加载图像的最佳实践

c# - 如何正确地将日期时间从 c# 传递到 sql?

php - 你将如何在没有循环的情况下重写它?

mysql - 使用内部查询进行查询优化