2013-02-08 57 views
3

活动记录我想要的原始查询到这里来插入是:使用WHERE CONCAT与笨

SELECT * FROM x WHERE CONCAT(y, ' ', x) LIKE '%value%'; 

我已经通过AR文档检查并不能发现什么,让我去做这个。我不太了解它是如何构建这些查询的,并且希望有人能够指引我朝着正确的方向发展。谢谢一堆。

回答

8

如果你想使用你需要传递FALSE的AR类作为第三个参数,以避免被自动转义查询。您现在可以自己逃避论证:

$value = $this->db->escape_like_str($unescaped); 

$this->db->from('x'); 
$this->db->where("CONCAT(y, ' ', x) LIKE '%".$value."%'", NULL, FALSE); 
$result = $this->db->get(); 

请参阅手册的Active Record会话中的第4)点。引用:

Custom string: 

    You can write your own clauses manually: 
    $where = "name='Joe' AND status='boss' OR status='active'"; 
    $this->db->where($where); 
    $this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. 
    $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE); 

一个简单的方法,恕我直言,对子级是运行一个“正规”的查询,并采取结合的优势:不使用第三个参数

$result = $this->db->query("CONCAT(y, ' ', x) LIKE '%?%'", array($value)); 
0

这样的事情应该工作:

$this->db->where("CONCAT(y, ' ', x) LIKE '%value%'"); 

$this->db->get(x); 
+0

这是行不通的,表达将与反引号进行转义 – 2013-02-08 21:58:55

0

或者使用关联数组方法:

$a = array(
    'CONCAT(`y`, " ", `x`)' => $value, 
    'title' => $title, 
    ... 
); 
... 
$this->db->like($a); 

将产生在查询的一部分:
... WHERE CONCAT(`y`, " ", `x`) LIKE '%test value%' AND `title` LIKE '%test title%' AND ...
鄂毕在使用多个搜索参数时非常有用。

0

这是旧的,但...

你可以试试这个:

$this->db->like('CONCAT(field_name," ",field_name_b)',$this->db->escape_like_str('value'));