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

[已解决] 新手求教:CI里面where的OR判断怎么写?

[复制链接]
发表于 2011-9-24 12:00:13 | 显示全部楼层 |阅读模式
$this->db->where('cityid', 'beijing');
这一句,现在我是需要cityid = beijing or cityid = xiamen

请问该如何写呢?感谢
发表于 2016-12-14 15:42:23 | 显示全部楼层
yanchunxia 发表于 2016-12-14 12:02
where type='city' and (cityid='beijing' or cityid='xiamen')想要得到这种结果ci框架应该怎么写 ...

QueryBuilder 不是万能的,建议复杂 SQL 直接用 query()
发表于 2016-12-14 12:02:11 | 显示全部楼层
where type='city' and (cityid='beijing' or cityid='xiamen')想要得到这种结果ci框架应该怎么写
发表于 2011-9-24 12:51:11 | 显示全部楼层
$this->db->or_where();
发表于 2011-9-24 14:31:31 | 显示全部楼层
1.自定义字符串:
$where =”cityid = ‘beijing’ OR cityid = 'xiamen'“;

$this->db->where($where);
或:
2.使用or_where
$this->db->where('cityid ', 'beijing');
$this->db->or_where('cityid', 'xiamen');
发表于 2011-9-24 19:24:05 | 显示全部楼层
感谢楼上,试试!
发表于 2011-11-30 11:07:30 | 显示全部楼层
mark.
发表于 2011-12-2 14:44:25 | 显示全部楼层
$this->db->where('cityid ', 'beijing');
$this->db->or_where('cityid', 'xiamen');

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

$this->db->where('type', 'city');
$this->db->where('cityid ', 'beijing');
$this->db->or_where('cityid', 'xiamen');

这样构成的sql语句是
where type='city' and cityid='beijing' or cityid='xiamen'
和下面的结果是不一样的
where type='city' and (cityid='beijing' or cityid='xiamen')

发表于 2011-12-2 14:46:03 | 显示全部楼层
看手册啊。。看手册。
发表于 2011-12-5 23:49:32 | 显示全部楼层
我觉得直接写sql语句方便啊,大家说说呢?还有那个table类也是这样,我直接写table表格,如果配合jquery,那就更方便了。
发表于 2012-4-10 14:58:15 | 显示全部楼层
乌有 发表于 2011-12-2 14:44
$this->db->where('cityid ', 'beijing');
$this->db->or_where('cityid', 'xiamen');

那遇到这种写法 怎么办呢?
发表于 2013-7-18 11:36:52 | 显示全部楼层
本帖最后由 bingdong700 于 2013-7-18 12:24 编辑
只想、静一静 发表于 2012-4-10 14:58
那遇到这种写法 怎么办呢?

  • 查看手册,$this->db->where();里面第四段文字
  • 自定义字符串:你可以手动的编写子句:$where = "name='Joe' AND status='boss' OR status='active'";$this->db->where($where);


本版积分规则