0

codeigniter使用しています。WHERE句でIF条件を使用したいと思います。しかし、それは正しく実行されていません。

$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 nameおよびfirst name concat によってlast name

値を確認しますc.PrivacySettingsElephantiUser。それだけが本当なら、私は姓と名、姓の連結値を探しています、それはc.PrivacySettingsElephantiUser=1AND c.PrivacySettingsElephantiUser=0

この部分は実行されていません。

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

if条件を無視して、常に名で検索するだけです。

これを正しく書く方法は?

AND ,ORロジックを使用してこれを行う簡単な方法はありますか?

アップデート

これは私の完全なクエリです

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; 
}
}
4

1 に答える 1

1

SQLコードを書くだけでPHP操作を取得しない..whereクエリでCaseステートメントを使用する

case when (c.PrivacySettingElephantiUser=1 AND (a.LastName LIKE '%something%' 
OR CONCAT(a.FirstName, \' \', a.LastName) like '%something%') then 1 else 0 end;
于 2012-07-09T06:07:42.433 に答える