codeigniter - database : how to update multiple tables with a single update query(codeigniter - 数据库:如何使用单个更新查询更新多个表)
问题描述
我在 codeigniter 论坛上看到了这个
I saw this on the codeigniter forum
考虑下面的代码
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 的 Active Record 数据库类通过连接进行更新?
推荐答案
我发现的一个解决方案是完全删除连接并将连接条件移动到 'where' 函数中,您还需要将更新字符串更改为包括新表.
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 - 数据库:如何使用单个更新查询更新多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:codeigniter - 数据库:如何使用单个更新查询更新多个表
				
        
 
            
        - 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
 - 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
 - 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
 - 在SQL中,如何为每个组选择前2行 2021-01-01
 - 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
 - 更改自动增量起始编号? 2021-01-01
 - 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
 - 导入具有可变标题的 Excel 文件 2021-01-01
 - SQL 临时表问题 2022-01-01
 - 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
 
						
						
						
						
						
				
				
				
				