MySQL查询代码使用OR为两个不同的字段与Web服务


MySQL query code using the OR for two different fields with Web Service

通常我可以弄清楚,但格式看起来有点不同,然后我习惯了。我有一个使用CodeIgnighter的php web服务。函数看起来像这样:

    function getCurrentSales($office_id)
    {
        $CI =& get_instance();
        $CI->load->model("properties");
        //Get the properties
        $where = array('field_SaleOfficeCode'=>$office_id);
        $where = array('field_ListOfficeCode'=>$office_id);
        $result = $CI->properties->getCurrentSales($where); 
        $properties = $result->result_array();
        foreach($properties as $p){
            //Get property images
            $where = array('ListingKey'=>$p['UniqueKey']);
            $property_arr[] = $p;
        }   
            return  $property_arr;
    }
    $this->nusoap_server->service(file_get_contents("php://input"));
} 

我要做的是返回field_ListOfficeCodefield_ListOfficeCode具有$office_id值的行。就像现在一样,只有当field_ListOfficeCode=>$office_id,而不是当其中一个等于值。

模型方法代码:

function getCurrentSales($where = null) {

    $this->db->select('field_ListingKey as UniqueKey,
                        field_LocaleListingStatus as Status,
                        field_CloseDate as ClosingDate,
                        field_ContractDate as ContractDate,
                        field_ListingID as MLS,
                        TRIM("#VARIES" from field_FullStreetAddress) as StreetAddress,
                        field_ListPrice as Price,
                        field_ListOfficeCode as BrokerID,
                        if(field_Beds != "", field_Beds, 0) as NumBeds,
                        if(field_BathsFull != "", field_BathsFull, 0) as NumFullBaths,
                        if(field_BathsHalf != "", field_BathsHalf, 0) as NumHalfBaths,
                        field_InternetRemarks as PropertyRemarks,
                        field_ListOfficeName as ListingOfficeName,
                        field_ListPicture3URL as MainPhoto,
                        CONCAT(field_ListAgentNickname, " ", field_ListAgentLastName, " ", (if(field_ListAgentNameSuffix = "NULL", field_ListAgentNameSuffix, ""))) as ListingAgent,
                        CONCAT(field_SaleAgentNickname, " ", field_SaleAgentLastName, " ", (if(field_SaleAgentNameSuffix = "NULL", field_SaleAgentNameSuffix, ""))) as SalesAgent,
                        CONCAT(field_City, ", ", field_State, " ", field_PostalCode) as AreaAddress', false);

    $this->db->from('TABLE_2');

    if($where != null){
        $this->db->where($where, false);
        $this->db->where('YEAR(field_ContractDate) = YEAR(NOW())', NULL, false);
        $this->db->where('MONTH(field_ContractDate) = MONTH(NOW())', NULL, false);
    }

    $this->db->order_by('ContractDate', 'desc'); //get random row
    //$this->db->limit($limit);

    $query = $this->db->get();
    return $query;     
} 

我不熟悉CodeIgniter,但我怀疑您的问题是这两行

$where = array('field_SaleOfficeCode'=>$office_id);
$where = array('field_ListOfficeCode'=>$office_id);

您实际上并没有构建where子句的数组,而是用第二个赋值覆盖第一个赋值,这解释了为什么您只得到匹配field_ListOfficeCode == $office_id

的结果。

根据CodeIgniter期望这些where子句如何形成,我假设它应该是像

这样的东西
$where = array(
    array('field_SaleOfficeCode'=>$office_id),
    array('field_ListOfficeCode'=>$office_id)
)

或者快速浏览一下文档

$where = array(
    'field_SaleOfficeCode'=>$office_id,
    'field_ListOfficeCode'=>$office_id
)

然后在getCurrentSales函数中,它应该是

$this->db->or_where($where)