![](/img/trans.png)
[英]Multi-WHERE for two fields in different MySQL tables for a SELECT query
[英]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_ListOfficeCode
或field_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.