深深的呼吸 发表于 2013-7-6 10:11:18

CodeIgniter如何生成这样的sql语句

本帖最后由 深深的呼吸 于 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连接,怎么弄?

我是这样的写的:

                $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达到这样的效果?

qi_ruo 发表于 2013-7-6 12:08:41

$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 static/image/common/back.gif


{:soso_e183:}
页: [1]
查看完整版本: CodeIgniter如何生成这样的sql语句