用户
 找回密码
 入住 CI 中国社区
搜索
查看: 20731|回复: 9
收起左侧

[HELP] CI where多重条件查询

[复制链接]
发表于 2015-8-3 09:47:31 | 显示全部楼层 |阅读模式
表字段 uid,name, tags,status  
这里需要用到name和tags的模糊查询
status的查询

$this->db->where('uid ', '1');
$this->db->or_where('status', '5');

$this->db->like('name', 'na');
$this->db->or_like('tag', 'na');

这样的sql是  
where uid=1 and status=5 and ( name like '%na%' or  tag like '%na%' )

只是这两个条件的话,没有问题.但如果加多了一个and的条件,这种写法就不行了,如

$this->db->where('uid ', '1');
$this->db->where('status', '1');
$this->db->or_where('status', '5');

$this->db->like('name', 'na');
$this->db->or_like('tag', 'na');

这样构成的sql语句是
where uid=1 and status=1 or status=5 and ( name like '%na%' or  tag like '%na%' )
这样查询sql 自动识别成了 where (uid=1 and status=1) or status=5 and ( name like '%na%' or  tag like '%na%' )
会查出 库里status=5的值 uid判断就失去作用

想要的到的结果应该是
where uid=1 and (status=1 or status=5)  and ( name like '%na%' or  tag like '%na%' )

框架里 也没又发现什么方法能够解决


发表于 2015-8-4 11:29:19 | 显示全部楼层
至于这个问题并不清楚怎么做
但是可能你可以优化一下查询
uid从命名来看应该是一个唯一的吧,那就没必要带后面那么多条件了
自己把uid=1的查出来,用php来判断其他条件吧
发表于 2015-8-13 18:33:36 | 显示全部楼层
楼主这个问题解决了没?今天也遇到,大家共同开发项目,也不能贸然修改CI数据库代码?求解
发表于 2015-8-24 11:27:09 | 显示全部楼层
发表于 2015-12-29 17:01:29 | 显示全部楼层
coolhpy 发表于 2015-8-24 11:27
**** 作者被禁止或删除 内容自动屏蔽 ****

你好,3.0好像才有查询条件组,我的项目用的是2.0,有什么解决方法吗
发表于 2016-3-21 11:48:43 | 显示全部楼层
一叶倾心 发表于 2015-12-29 17:01
你好,3.0好像才有查询条件组,我的项目用的是2.0,有什么解决方法吗

复杂的查询直接写SQL语句还方便
发表于 2016-5-24 16:34:06 | 显示全部楼层
是啊。如果不写条件组,还不好实现。
发表于 2016-5-24 16:35:14 | 显示全部楼层
好二啊,where和or_where配对,like和or_like配对。where和or_like,却不能配对成功。
发表于 2016-5-24 17:14:57 | 显示全部楼层
可以自己写sql。然后调用框架的 $query = $db->query($sql);也可以达到效果。自己写sql可以想与就与,想或就或。只是看起来不怎么舒服。
发表于 2016-5-24 22:01:59 | 显示全部楼层
我这儿直接报  Message: Call to a member function result_array() on a non-object 这个错误

本版积分规则