【问题标题】:I need to compare two different columns in different rows using mysql我需要使用mysql比较不同行中的两个不同列
【发布时间】:2016-04-29 15:15:15
【问题描述】:

我需要比较不同行中的两列 l_area 和 d_area。

如果第1行的l_area等于第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;
 }  

【问题讨论】:

  • 你能显示打印查询生成了什么查询吗?
  • 为什么不使用 $where='d_area=l_area AND consignment.status=0';

标签: php mysql codeigniter


【解决方案1】:

我认为您需要在这里加入:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多