2012-07-09 38 views
0

我正在使用codeigniter,我想在WHERE子句中使用IF条件。但它没有正确执行。如何在WHERE子句中正确写入IF条件codeigniter

$where_string="(a.FirstName LIKE '".$search_phrase."%' OR (IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))"; 
$this->db->where($where_string); 

我在这里做一个搜索。通过first namelast namefirst nameconcatlast name

我检查值c.PrivacySettingsElephantiUser。如果只是这是真的,我为姓和名字cheque的姓氏concat值,事情是如果c.PrivacySettingsElephantiUser=1c.PrivacySettingsElephantiUser=0,

这部分不执行。

OR (IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')

它仅由第一名称搜索总是忽略if条件,

如何正确地写这个?

有没有一种简单的方法来使用AND ,OR逻辑来做到这一点?

UPDATE

这是我的完整的查询

public function search_people($profile_id,$search_phrase=NULL,$country=NULL,$state=NULL,$city=NULL,$neighborhood=NULL,$type=NULL,$limit=NULL,$offset=NULL) 
{ 
    $this->db->select('SQL_CALC_FOUND_ROWS a.ProfileID,a.FirstName,a.LastName,a.StateName,a.CityName,a.ShowLastName,a.UserType,a.ProfileImg,b.FriendStatus,b.RequesterID,b.AccepterID',FALSE); 
    $this->db->from($this->mastables['xxxxxxxx'].' as a'); 
$this->db->join($this->mastables['yyyyyyyyyyy'].' as b'," b.RequesterID=a.ProfileID AND b.AccepterID=".$profile_id." OR b.AccepterID=a.ProfileID AND b.RequesterID=".$profile_id,'left'); 


    $this->db->where('a.ProfileID !=',$profile_id); 
    $this->db->where('a.UserType',2); 

    if($type=="friend") 
    { 

     $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner'); 
     $where_string="(a.FirstName LIKE '".$search_phrase."%' OR (IF(c.PrivacySettingFriend=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))"; 
     $this->db->where($where_string); 
     $this->db->where('b.FriendStatus',1); 
    } 
    else if($type=="other") 
    { 
     $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner'); 
     $where_string="(a.FirstName LIKE '".$search_phrase."%' OR (IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))"; 
     $this->db->where($where_string); 
     $this->db->where ('(b.FriendStatus IS NULL OR b.FriendStatus=0)'); 
    } else {  
     $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner'); 
     $where_string="(a.FirstName LIKE '".$search_phrase."%' OR (IF(c.PrivacySettingElephantiUser=1 OR c.PrivacySettingFriend=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\'','')))"; 
     //$where_string="(a.FirstName LIKE '".$search_phrase."%' OR (IF(c.PrivacySettingElephantiUser=1 OR c.PrivacySettingFriend=1,'a.LastName LIKE b%','')))"; 
     $this->db->where($where_string); 
    } 

    if($country) 
    { 
     $this->db->where('a.CountryID',$country); 
    } 
    if($state) 
    { 
     $this->db->where('a.StateID',$state); 
    } 
    if($city) 
    { 
     $this->db->where('a.CityID',$city); 
    } 
    if($neighborhood) 
    { 
     //$this->db->where('',$neighborhood); 
    } 
    if($limit) 
    { 
     $this->db->limit($limit,$offset); 
    } 

$query = $this->db->get(); 
//echo $this->db->last_query();die; 
$result['result'] = $query->result_array(); 

$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`'); 
    $result["totalrows"] = $query->row()->Count; 

if(!empty($result)) 
{ 
    return $result; 
} 
} 
+0

我想你可以”在if条件中使用'sql表达式',可能是因为使用'CASE'而不是@manurajhada表示 – Junaid 2012-07-09 06:18:38

+0

@junaid请告诉我,怎么样,我试过,但给人错误 – 2012-07-09 06:21:45

+0

尝试这个 - '$ where_string = “(a.FirstName LIKE“”。$ SEARCH_PHRASE。“%'OR( \t CASE \t \t WHEN c.PrivacySettingElephantiUser = 1,则 \t \t \t 'a.LastName LIKE \' “$ SEARCH_PHRASE。” %\ 'OR \t \t \t \t CONCAT(a.FirstName,\' \ 'a.LastName)如\' “$ SEARCH_PHRASE。” %\”' \t \t \t \t ELSE '' \t \t \t \t)));; – Junaid 2012-07-09 07:04:08

回答

1

没有得到PHP操作只是写的SQL代码..使用Case语句进行查询哪里

case when (c.PrivacySettingElephantiUser=1 AND (a.LastName LIKE '%something%' 
OR CONCAT(a.FirstName, \' \', a.LastName) like '%something%') then 1 else 0 end; 
+0

嗯,如何将这个应用到我的代码中,我试过了\t \t \t $ where_string =“(a.FirstName LIKE'”。$ search_phrase。“%'OR(case(when.CustomersSettingElephantiUser = 1 AND(a.LastName LIKE'“。$ search_phrase。”%'OR CONCAT(a.FirstName,\'\',a.LastName)like'“。$ search_phrase。”%')then 1 else 0 end) ))“; 但它失败 – 2012-07-09 06:18:38

+0

尝试此操作.. '$ where_string =“(a.FirstName LIKE'”。$ search_phrase。“%'OR(case)(c.PrivacySettingElephantiUser = 1 AND(a.LastName LIKE'”。$ search_phrase。“%'OR CONCAT(a.FirstName,\'\',a.LastName)like'”。$ search_phrase。“%'))then 1 else 0 end))”;' – manurajhada 2012-07-09 06:26:06

+0

当CONCAT (a.FirstName,\'\',a.LastName)。非常感谢你 。 – 2012-07-09 07:19:59