这就是我在查询中使用 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/