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

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

问题描述

我有两个桌子

  1. 表1
  2. table2

Tabel1包含2列

Tabel1 contains 2 columns

  1. id
  2. 名称

Tabel2包含2列

Tabel2 contains 2 columns

  1. id
  2. 年龄

一个想要将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. id
  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天全站免登陆