[英]How to make the php codeIgniter active record group?
First, I have something in my model: 首先,我的模型中有一些东西:
$this->db->where('status', $aStatus);
$this->db->limit($aLimit, $aOffset);
$this->db->or_like('title', $aString);
$this->db->or_like('description', $aString);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
And I got this query: 我收到了这个问题:
SELECT
id
,description
,title
,fee
FROM (events
) WHEREstatus
= 0 ANDtitle
LIKE '%space%' ORdescription
LIKE '%space%' LIMIT 5SELECT
id
,description
,title
,fee
FROM(events
)WHEREstatus
= 0 ANDtitle
LIKE'%space%'ORdescription
LIKE'%space%'LIMIT 5
But I would like to let it generate this query instead 但我想让它生成这个查询
SELECT
id
,description
,title
,fee
FROM (events
) WHEREstatus
= 0 AND (title
LIKE '%space%' ORdescription
LIKE '%space%' ) LIMIT 5SELECT
id
,description
,title
,fee
FROM(events
)WHEREstatus
= 0 AND (title
LIKE'%space%'或description
LIKE'%space%' ) LIMIT 5
How can I modify to do so? 我该怎么修改呢? Thank you.
谢谢。
Grouping LIKE clause (putting parenthesis around) 分组LIKE子句(括号括起来)
$this->db->group_start();
$this->db->like('name', $query_array['q']);
$this->db->or_like('description', $query_array['q']);
$this->db->group_end();
Will produce round backets like this 会产生这样的圆形支撑
(
`name` LIKE '%test%' ESCAPE '!'
OR `description` LIKE '%test%' ESCAPE '!'
)
Think the only way you'll manage that is by doing something along the lines of: 认为你要管理的唯一方法就是做一些事情:
$this->db->where('status', $aStatus);
$this->db->where("(title LIKE '%space%' OR description LIKE '%space%')");
$this->db->limit($aLimit, $aOffset);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
Haven't tested it but it should get you on the way. 没有测试过,但它应该让你在路上。 Other than that I don't think it's possible to include a grouped/bracketed query any other way.
除此之外,我认为不可能以任何其他方式包括分组/括号查询。
I would just do something like 我会做类似的事情
$this->db->where('status', $aStatus);
$this->db->limit($aLimit, $aOffset);
$this->db->where(sprintf(
'(title LIKE %s OR description LIKE %s)',
$this->db->escape_like_str($aString),
$this->db->escape_like_str($aString)
), NULL);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.