首页 > 解决方案 > 如何在 Codeigniter 中使用 Join 进行更新查询

问题描述

我在下面给出了我的 Codeigniter 代码,这里我需要使用连接条件更新记录。我使用了下面的代码,但显示错误

$condition="a.assignto='0' and a.recstatus='1' and b.location='$location' and '$category' IN(SELECT categoryid FROM `tq_productcategory` where productid=c.productid and recstatus='1')";
$this->db->set($data);
$this->db->where($condition);
$this->db->limit($limit);
$this->db->join('tq_customer b','a.customerid=b.customerid');
$this->db->join('tq_product c','a.productid=c.productid');
$this->db->order_by("a.created_on", "asc");
$this->db->update('tq_customerservicesupport a');

以下是我的错误消息

 Unknown column 'b.location' in 'where clause'

UPDATE `tq_customerservicesupport` `a` SET `assignto` = '10' WHERE `a`.`assignto` = '0' and `a`.`recstatus` = '1' and `b`.`location` = '3227' and '1' IN(SELECT categoryid FROM `tq_productcategory` where productid = `c`.`productid` and `recstatus` = '1') ORDER BY `a`.`created_on` ASC LIMIT 1

Filename: D:/wamp/www/tooquik/system/database/DB_driver.php

标签: phpmysqlcodeigniter

解决方案


试试这个:

$sql = "UPDATE tq_customerservicesupport AS a JOIN tq_customer AS b ON a.customerid = b.customerid JOIN tq_product AS c ON  a.productid = c.productid SET $data WHERE $condition ORDER BY a.created_on ASC LIMIT $limit";
$this->db->query($sql);

确保查询是正确的,因为我只是根据您的数据编写的,最好在 phpmyadmin 中检查它以确保它正常工作且没有错误。


推荐阅读