2016-04-29 55 views
0

我需要比较不同行中的两列l_area和d_area。我需要使用mysql来比较不同行中的两个不同列

需要输出代销表的结果,如果l_area第1行中的第2行

下面等于d_area是我的模型,但其检查同一行

public function return_loads() 
{ 
    $where='d_area=l_area'; 
    $this->db->select('*'); 
    $this->db->from('consignment');   
    $this->db->where('consignment.status',0); 
    $this->db->where($where);  
    $query = $this->db->get();  
    return $query; 
} 
+0

你能通过打印查询显示生成的查询吗? –

+0

以及为什么不使用$ where ='d_area = l_area AND consignment.status = 0'; –

回答

0

我想你需要一个加入此处:

SELECT c.* 
FROM consignement c 
INNER JOIN consignement c2 
    ON c.d_area = c2.d_area 
WHERE c.consignement_status=0 
-- AND c2.consignement_status=0 

不确定关于where子句的最后部分,它取决于您的特定用例。这是原始SQL,但你可以使用codeIngiter的join()为:

public function return_loads() 
{ 
    $where='d_area=l_area'; 
    $this->db->select('c1.*'); 
    $this->db->from('consignment c1'); 
    $this->db->join('consignement c2', 'c1.l_area = c2.d_area', 'inner'); 
    $this->db->where('c1.status',0); 
    $this->db->where($where); -- use 'c1' instead of 'consignement' in your where clause 
    $query = $this->db->get();  
    return $query; 
} 

你也可以看看this question

相关问题