ENG:
大家好, 问题,CodeIgniter ORM active_record因为我可以生成SQL例如..?
示例生成器SQL: ... AND(LIKE ......或LIKE ......)
SELECT id, c1, c2 FORM t1 WHERE id ='1' AND ( c1 LIKE '%3%' OR c2 LIKE '%3%' )
不以组合ORM函数的形式包含,以生成我在SQL实例中显示的内容。
这是什么,但大部分死亡不是我想要的。
$this->db->select('id, c1, c2')
->from('t1')
->where('id', '1111')
->like('c1',$sSearch)
->or_like('c2',$sSearch)
->get();
如果有人能给我一个命令,将非常感激。 问候
ESP:
Hola a todos, Pregunta,con CodeIgniter el ORM o active_record como puedo generar el SQL del ejemplo ..?
Ejemplo SQL: ... AND(喜欢......或者......)
SELECT id, c1, c2 FORM t1 WHERE id ='1' AND ( c1 LIKE '%3%' OR c2 LIKE '%3%' )
No enccontre en el ORM la forma de combiar las funciones para generar algo como lo que muestro en el SQL de ejemplo。
Lo mas perecido al SQL es esto pero no es lo que busco。
$this->db->select('id, c1, c2')
->from('t1')
->where('id', '1111')
->like('c1',$sSearch)
->or_like('c2',$sSearch)
->get();
Si alguien me puede dar una mando estaria muy agradecido。
Saludos
答案 0 :(得分:1)
混合AND
和OR
时,括号是您的朋友。 CodeIgniter的有效记录不支持您想要的AND
和OR
混合。
CodeIgniter将输出以下查询:
SELECT id, c1, c2 FORM t1 WHERE id ='1' AND c1 LIKE '%3%' OR c2 LIKE '%3%'
显然,这不是你想要的。您可以做的是:将自定义字符串传递给WHERE
以将其用作子句。
// Since we are using a custom string, we need to escape manually
$sSearch = $this->db->escape_like_str($sSearch);
$this->db->select('id, c1, c2')
->from('t1')
->where('id', '1')
// this will add the clause like you want
->where("(`c1` LIKE '%$sSearch%' OR `c2` LIKE '%$sSearch%')", NULL, FALSE)
->get();
这将输出您想要的查询:
SELECT id, c1, c2 FORM t1 WHERE id ='1' AND (c1 LIKE '%3%' OR c2 LIKE '%3%')
答案 1 :(得分:0)
由于你的英语很难理解,我正在捅这个......
我假设您在关键字之前或之后尝试使用LIKE和通配符%
。
你可以在之前,之后,两者(默认)中使用like或or_like ..
<?php
$this->db
->select('id, c1, c2')
->from('t1')
->where('id', '1111')
//$this->db->like('title', 'match', 'before');
//Produces: WHERE title LIKE '%match'
//$this->db->like('title', 'match', 'after');
//Produces: WHERE title LIKE 'match%'
//$this->db->like('title', 'match', 'both');
//Produces: WHERE title LIKE '%match%'
->like('c1', $sSearch)
->or_like('c2', $sSearch)
->get();
或者您可以使用原始sql执行 - http://codeigniter.com/user_guide/database/queries.html
<?php $this->db->query("YOUR SEQUEL STATEMENT HERE");