【问题标题】:How to write this IF condition correctly, inside WHERE clause codeigniter如何在 WHERE 子句 codeigniter 中正确编写此 IF 条件
【发布时间】:2012-07-09 05:57:05
【问题描述】:

我正在使用 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 namefirst name concat last name

我检查了 c.PrivacySettingsElephantiUser 的值。如果它是真的,我正在检查姓氏和名字姓氏连接值,如果c.PrivacySettingsElephantiUser=1 ANDc.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; 
}
}

【问题讨论】:

  • 我认为你不能在 if 条件下使用sql expressions,可能这就是@manurajhada 所说的使用CASE 的原因
  • @junaid,请告诉我,如何,我尝试过,但报错
  • 试试这个-$where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( CASE WHEN c.PrivacySettingElephantiUser = 1 THEN 'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ' ELSE '' )))";

标签: mysql sql codeigniter where-clause


【解决方案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;

【讨论】:

  • hmm,如何将它应用到我的代码中,我通过 $where_string="( a.FirstName LIKE '".$search_phrase."%' 或(c.PrivacySettingElephantiUser=1 AND (a.LastName LIKE '".$search_phrase."%' OR CONCAT(a.FirstName, \' \', a.LastName) like '".$search_phrase."%') then 1 else 0 end )))" ;但它失败了
  • 试试这个..$where_string="( a.FirstName LIKE '".$search_phrase."%' OR (case when (c.PrivacySettingElephantiUser=1 AND (a.LastName LIKE '".$search_phrase."%' OR CONCAT(a.FirstName, \' \', a.LastName) like '".$search_phrase."%')) then 1 else 0 end ))";
  • 在 CONCAT(a.FirstName, \' \', a.LastName) 中删除 \ 时有效。非常感谢。
猜你喜欢
  • 2022-01-14
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多