如何从 Mysql 中的另一个表中将列添加到表中? [英] How to add a column to a table from another table in Mysql?

查看:33
本文介绍了如何从 Mysql 中的另一个表中将列添加到表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子

  1. table1
  2. table2

Tabel1 包含 2 列

Tabel1 contains 2 columns

  1. 身份证
  2. 姓名

Tabel2 包含 2 列

Tabel2 contains 2 columns

  1. 身份证
  2. 年龄

A 想将 table2 中的 age 列添加到 table1 (WHERE table1.id = table2.id)

A want to add age column from table2 to table1 (WHERE table1.id = table2.id)

那么 table1 应该包含 3 列

Then table1 should contains 3 columns

  1. 身份证
  2. 姓名
  3. 年龄

推荐答案

先在table1中添加Age列

First add Age column in table1

ALTER TABLE table1 ADD COLUMN Age TINYINT UNSIGNED DEFAULT 0;

然后使用打击查询更新该列

then update that column using blow query

UPDATE table1 t1
INNER JOIN Tabel2 t2 ON t1.id = t2.id 
SET t1.age = t2.age;

这篇关于如何从 Mysql 中的另一个表中将列添加到表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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