Codeigniter更新功能中的两个不同的表名

时间:2015-09-12 19:59:34

标签: php mysql codeigniter

基本信息 Codeigniter:3,PHP 5

我目前正在一个更新查询中进行多表更新。我的SQL如下 -

UPDATE users as a, u_basicinfos as b
SET 
    a.first_name = 'XYI',
    b.Address = 'USA',
    b.PortalURL = NULL
WHERE a.id = b.UserId AND b.InfosId = 1

以上查询绝对适用于heidisqlphpmyadmin sql console。

我正在尝试在codeigniter中创建active records查询,如下所示 -

$this->db->where('a.id = b.UserId');
$this->db->where('b.Infosid', $infoid);

$tblname = 'users AS a, u_basicinfos AS b';
// may be problem with this format of two tables name

$query = $this->db->update($tblname, $data);

作为备注:我在Controller中分配了$data数组:

$data = array (
    'a.first_name' => $this->input->post('first_name'),
    'b.Address' => $this->input->post('Address'),
    'b.NewportalURL' => $this->input->post('PortalURL')
);

现在,我正在努力解决错误。

Table 'finalportal.users as a, u_basicinfos' doesn't exist

UPDATE `users AS a, u_basicinfos` AS `b` SET `a`.`first_name` = 'XYI',
`b`.`Address` = 'USA', `b`.`PortalURL` = NULL

我需要一些关于此的想法。让我知道我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

尝试在查询中使用此功能

$this->db->where('b.Infosid', $infoid);
$tblname = 'users a JOIN u_basicinfos b on a.id = b.UserId';
$query = $this->db->update($tblname, $data);