惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 星畔

解决网站访问突然变成“403禁止访问:访问被拒绝” 解决openclaw + 飞书群不@所有人,群里其他人不回复的问题 针对 WSL 环境的特殊处理(如果你用的是 Windows 子系统) wsl 删除ubuntu-24.04 window 站点 vue3编译后的项目刷新页面404解决办法 ASP.NET Core Web API 需要先发布到 IIS 服务器才能运行 使用 .NET Core。如果目标进程未运行 .NET Core,则发生这种情况并不意外。 因 Cookie 被添加了 SameSite=None 属性导致在非 https 环境下无法为网站正确设置 Cookie 进而导致系统状态异常的问 完美解决:没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限。 - 星畔 Windows10 IIS Web服务器安装配置 解决“远程桌面连接:出现身份验证错误,要求的函数不受支持。。。 ” elasticsearch 请求被中止: 未能创建 SSL/TLS 安全通道”的原因及解决办法 从 bcp 客户端收到一个对 colid x 无效的列长度。 log4j的配置ConversionPattern详细讲解 SQL 更改字段长度 SQL Server(00):表压缩 Vue网站发布到iis后提示404页面不可访问 C#中四舍五入的正确写法是什么?
mysql根据一个表的数据更新另一个表数据的SQL写法
星畔 · 2024-11-06 · via 博客园 - 星畔
ql 中更新表数据的通用方法包括:使用 join 语句,匹配两个表并更新目标表的指定列;使用子查询,获取源表中匹配行的值并更新目标表;使用 merge 语句(mysql 8.0 及更高版本),合并两个表并按条件更新或插入数据。

根据一个表的数据更新另一个表数据的 SQL 写法

最近大家都在看

MySQL update 命令的详细用法

怎么查看mysql的锁表

mysql如何将字符串转换成数字

方法一:使用 JOIN 语句

1

2

UPDATE table2 SET column2 = table1.column1

JOIN table1 ON table2.id = table1.id;

方法二:使用子查询

1

2

3

4

5

6

UPDATE table2

SET column2 = (

    SELECT column1

    FROM table1

    WHERE table2.id = table1.id

);

方法三:使用 MERGE 语句(MySQL 8.0 及更高版本)

1

2

3

4

5

MERGE INTO table2 AS t2

USING table1 AS t1

ON t2.id = t1.id

WHEN MATCHED THEN

  UPDATE SET column2 = t1.column1;

示例

假设有以下两个表:

  • table1:包含具有 id 和 name 列的数据
  • table2:包含具有 id 和 description 列的数据

要使用 table1 中的 name 更新 table2 中的 description,可以使用以下 SQL 查询:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

-- 使用 JOIN 语句

UPDATE table2 SET description = table1.name

JOIN table1 ON table2.id = table1.id;

-- 使用子查询

UPDATE table2

SET description = (

    SELECT name

    FROM table1

    WHERE table2.id = table1.id

);

-- 使用 MERGE 语句(MySQL 8.0 及更高版本)

MERGE INTO table2 AS t2

USING table1 AS t1

ON t2.id = t1.id

WHEN MATCHED THEN

  UPDATE SET description = t1.name;