Update table values from another table with the same user name(从另一个具有相同用户名的表中更新表值)
问题描述
我有两个表,有一个名为 user_name
的列,分别是 table_a
、table_b
.
I have two tables, with a same column named user_name
, saying table_a
, table_b
.
我想,从table_b
、column_b_1
、column_b2
复制到table_b1
、column_a_1
、column_a_2
,分别是user_name
相同的地方,在SQL语句中怎么做?
I want to, copy from table_b
, column_b_1
, column_b2
, to table_b1
, column_a_1
, column_a_2
, respectively, where the user_name
is the same, how to do it in SQL statement?
推荐答案
只要你有合适的索引,这应该没问题:
As long as you have suitable indexes in place this should work alright:
UPDATE table_a
SET
column_a_1 = (SELECT table_b.column_b_1
FROM table_b
WHERE table_b.user_name = table_a.user_name )
, column_a_2 = (SELECT table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name )
WHERE
EXISTS (
SELECT *
FROM table_b
WHERE table_b.user_name = table_a.user_name
)
sqlite3 中的 UPDATE 不支持 FROM 子句,这使得它比在其他 RDBMS 中.
UPDATE in sqlite3 does not support a FROM clause, which makes this a little more work than in other RDBMS.
如果性能不令人满意,另一种选择可能是使用 select 为 table_a 构建新行并与 table_a 连接到临时表中.然后从table_a中删除数据并从临时数据中重新填充.
If performance is not satisfactory, another option might be to build up new rows for table_a using a select and join with table_a into a temporary table. Then delete the data from table_a and repopulate from the temporary.
这篇关于从另一个具有相同用户名的表中更新表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从另一个具有相同用户名的表中更新表值


- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01