php - MySQL 查询代码对 Web 服务的两个不同字段使用 OR

标签 php mysql arrays web-services

通常我可以解决这个问题,但格式看起来与我习惯的有点不同。我有一个使用 PHP 和 CodeIgnighter 的网络服务。该函数如下所示:

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

或快速查看 the documentation

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

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

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

关于php - MySQL 查询代码对 Web 服务的两个不同字段使用 OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15822651/

相关文章:

php - 是否可以在 WHERE 子句中使用 MySQL 字段值?

php - 为什么 openssl_public_encrypt 不能处理这个明文?

php - Yii 插入多条记录并返回 Id(INSERT IGNORE)

arrays - 无法分配 String 类型的值 :NSObject to type String:NSObject

php - WordPress taxonomy.php 不适用于自定义分类法

php - mysql表中插入的数据不按时间顺序排列

mysql - 计算MySQL中排名的变化

php - foreach 中的 SQL 更新仅更新数字而不更新文本

c - 如何使用 qsort 对依赖于另一个数组的数组进行排序?

javascript - 如何创建未调用的 Promise 函数的数组