2012-04-08 58 views
0

我試圖生成從那裏名字的姓的任意組合相匹配的特定搜索字詞Zend Framework的SQL SELECT查詢建設(WHERE子句)

$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 

什麼是正確的語法使用$選擇對象實現這一目標。

回答

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"); 
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); 
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);