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

[版本 2.x] CodeIgniter如何生成这样的sql语句

[复制链接]
发表于 2013-7-6 10:11:18 | 显示全部楼层 |阅读模式
本帖最后由 深深的呼吸 于 2013-7-6 10:12 编辑

我要通过CodeIgniter的Active Record生成一个查询语句,SELECT `ID`, `post_author`, `post_title`, `post_name` FROM (`tt_posts`) WHERE `post_author` = '徐巍' OR `post_title` LIKE '%徐巍%' ORDER BY `ID` desc LIMIT 8,即用where有用like,但是中间是OR连接,怎么弄?

我是这样的写的:
PHP复制代码
 
                $this->db->select('ID,post_author,post_title,post_name');
                $this->db->where('post_author',$author);
                $this->db->or_like('post_title',$author);
                $this->db->order_by('ID','desc');
                $result=$this->db->get('posts',8);
 
复制代码


生成的查询语句:

SELECT `ID`, `post_author`, `post_title`, `post_name` FROM (`tt_posts`) WHERE `post_author` = '徐巍' AND `post_title` LIKE '%徐巍%' ORDER BY `ID` desc LIMIT 8


我要求的sql语句是:
SELECT `ID`, `post_author`, `post_title`, `post_name` FROM (`tt_posts`) WHERE `post_author` = '徐巍' OR `post_title` LIKE '%徐巍%' ORDER BY `ID` desc LIMIT 8

试了几个方法了都不行,只能自己写完整的sql语句,怎样使用CodeIgniter的Active Record达到这样的效果?
发表于 2013-7-6 12:08:41 | 显示全部楼层
PHP复制代码
$this->db->where('post_author',$author);
$this->db->or_where('post_title LIKE','%'.$author.'%');
复制代码
 楼主| 发表于 2013-7-6 12:38:27 | 显示全部楼层
qi_ruo 发表于 2013-7-6 12:08

{:soso_e183:}

本版积分规则