codeigniter - 数据库:如何更新一次更新查询多个表 [英] codeigniter - database : how to update multiple tables with a single update query

查看:120
本文介绍了codeigniter - 数据库:如何更新一次更新查询多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在codeigniter论坛

I saw this on the codeigniter forum

考虑到以下code

UPDATE a
INNER JOIN b USING (id)
SET a.firstname='Pekka', a.lastname='Kuronen',
b.companyname='Suomi Oy',b.companyaddress='Mannerheimtie 123, Helsinki Suomi'
WHERE a.id=1; 

这是你会怎么做显然是在codeigniter

This is how you would apparently do it in Codeigniter

$this->db->set('a.firstname', 'Pekka');
$this->db->set('a.lastname', 'Kuronen');
$this->db->set('b.companyname', 'Suomi Oy');
$this->db->set('b.companyaddress', 'Mannerheimtie 123, Helsinki Suomi');
$this->db->where('a.id', 1);
$this->db->join('table2 as b', 'a.id = b.id');
$this->db->update('table as a');

这在现实中并不工作。我有一个看此产生,其结果甚至没有提到联接的SQL。

this does not work in reality. I have had a look a the SQL which this produces and the results do not even mention the join.

有没有人有任何想法如何做一个更新的连接使用codeigniter的活动记录数据库类?

推荐答案

一个解决方案,我发现是去除加盟共和移动连接条件成为一个位置功能,你也将需要更改更新字符串包括新表。

One solution I have found is to remove the join altogether and move the join condition into a 'where' function, also you will need to change the update string to include the new table.

$this->db->set('a.firstname', 'Pekka');
$this->db->set('a.lastname', 'Kuronen');
$this->db->set('b.companyname', 'Suomi Oy');
$this->db->set('b.companyaddress', 'Mannerheimtie 123, Helsinki Suomi');

$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->update('table as a, table2 as b');

这篇关于codeigniter - 数据库:如何更新一次更新查询多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆