0

我正在尝试生成一个查询,该查询从用户表中选择所有名字的姓氏的任何组合与特定的搜索词匹配

$select = $select->where('last_name LIKE ?', '%'.$term.'%')->orWhere('first_name LIKE ?', '%'.$term.'%')
                        ->orWhere("CONCAT(first_name,' ', last_name) LIKE ?", '%'.$term.'%')
                        ->orWhere("CONCAT(last_name,' ', first_name) LIKE ?", '%'.$term.'%');                           

还有一个条件也必须满足,它在另一个 where 子句中指定

$select = $select->where("deleted = 0 AND scholar = 0");

生成以下 SQL 语句

SELECT `user`.* FROM `user` WHERE (last_name LIKE '%frank%') OR (first_name LIKE '%frank%') OR (CONCAT(first_name,' ', last_name) LIKE '%frank%') OR (CONCAT(last_name,' ', first_name) LIKE '%frank%') AND (deleted = 0 AND scholar = 0) ORDER BY `date_created` desc LIMIT 25

这不会返回所需的结果,因为我得到了学者 = 1 的行;

我认为查询应该是

SELECT `user`.* FROM `user` WHERE ((last_name LIKE '%frank%') OR (first_name LIKE '%frank%') OR (CONCAT(first_name,' ', last_name) LIKE '%frank%') OR (CONCAT(last_name,' ', first_name) LIKE '%frank%')) AND  (deleted = 0 AND scholar = 0) ORDER BY `date_created` DESC LIMIT 25

使用 $select 对象实现此目的的正确语法是什么。

4

3 回答 3

3

您可以使用 quoteInto 来准备您的条件,然后像这样使用它们:

    $first_name_cond = $db->quoteInto('first_name LIKE ?', '%'.$term.'%');
    $last_name_cond = $db->quoteInto('last_name LIKE ?', '%'.$term.'%');

    $concat_cond1 = $db->quoteInto("CONCAT(first_name,' ', last_name) LIKE ?", '%'.$term.'%');

    $concat_cond2 = $db->quoteInto("CONCAT(last_name,' ', first_name) LIKE ?", '%'.$term.'%');


    $select = $select->where($first_name_cond.' OR '.$last_name_cond.' OR '.

             $concat_cond1.' OR '.$concat_cond2)->where("deleted = 0 AND scholar = 0");
于 2012-04-08T10:50:23.620 回答
0

我假设删除学者是单独的列。所以最简单的方法就是打破:

$select = $select->where("deleted = 0 AND scholar = 0");

分为两个语句,例如:

$select->where("deleted = ?", 0);
$select->where("scholar = ?", 0);

此更改应生成如下 sql 字符串:

SELECT `user`.* FROM `user` WHERE (last_name LIKE '%frank%')
OR (first_name LIKE '%frank%') 
OR (CONCAT(first_name,' ', last_name) LIKE '%frank%') 
OR (CONCAT(last_name,' ', first_name) LIKE '%frank%') 
AND deleted = 0 AND scholar = 0 ORDER BY `date_created` desc LIMIT 25

也去掉多余的$select =。您的整个选择应该看起来像:

//first line initializes the select object
//The select object will handle most quoting needs for you

$select = $this->select();

//I like to add expressions this way just to keep things easy to read and easy to edit
//you can string multiple statements together, but I find that harder to edit.

$select->where('last_name LIKE ?', '%'.$term.'%');
$select->orWhere('first_name LIKE ?', '%'.$term.'%');
$select->orWhere("CONCAT(first_name,' ', last_name) LIKE ?", '%'.$term.'%');
$select->orWhere("CONCAT(last_name,' ', first_name) LIKE ?", '%'.$term.'%');
$select->where("deleted = ?", 0);
$select->where("scholar = ?", 0);
$select->order('date_created DESC');
$select->limit(25);
于 2012-04-09T03:48:13.183 回答
0
$user = new Application_Model_DbTable_User();
            // User List

$uname=$_POST['uname'];

echo $query = $user->select()->where('firstname LIKE ?', $uname.'%')->ORwhere('lastname LIKE ?', $_POST['lname'].'%')->ORwhere('emailid LIKE ?', $_POST['email'].'%');

$userlist = $user->fetchAll($query);
于 2014-02-26T12:27:57.907 回答