Zend框架SQL选择查询构造(WHERE CLAUSE)

时间:2012-04-08 09:47:53

标签: sql zend-framework

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

$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

这不会返回所需的结果,因为我得到的行是scholar = 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对象实现此目的的正确语法。

3 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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);

答案 2 :(得分: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);