![](/img/trans.png)
[英]How can I compare 2 columns in different tables and insert unmatched records in a new table using MYSQL?
[英]I need to compare two different columns in different rows using mysql
我需要比较不同行中的两列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;
}
我认为您需要在这里加入:
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;
}
您也可以看看这个问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.