mysql - 如何使用另一个表中的新列更新现有表?
问题描述
我有两个数据库表。
**table 1:**
coupon_name || coupon_amount || coupon_id || coupon_status
**table 2:**
coupon_id || coupon_name || store_name
我想在表 2 中添加 coupon_amount 列。如何在 mysql 中执行此操作?
解决方案
表 2 没有“coupon_amount”列,因此首先您必须将其添加到表中。下一步是将表 1 中的记录添加到目标表中。您可以使用此查询:
ALTER TABLE 'table2'
ADD COLUMN 'coupon_amount' INT (or the type of the field you have in table 1);
然后,您需要执行第二步,可以是这样的查询:
UPDATE 'table2'
INNER JOIN 'table1' ON 'table2'.'coupon_id' = 'table1'.'coupon_id'
SET 'table2'.'coupon_amount' = 'table1'.'coupon_amount';
推荐阅读
- c# - 异步客户端套接字 C#。如何在不阻塞程序运行的情况下循环等待来自服务器的信息?
- java - 未在 xml ResourceNotFoundException 中加载矢量可绘制对象
- bash - 在 awk 中获取多个压缩文件的 FILENAME
- angular - Angular 7服务器端渲染同时服务:ssr
- javascript - Nest.js Jest 不能模拟测试应用,但可以模拟测试控制器
- html - 转换:translateY() 在 Safari 中不起作用
- reactjs - 使用谷歌地图地理定位 api 进行准确的地理定位
- java - 杰克逊 json 到 java 映射 + 切割 json 内部对象
- c# - SqlBulkCopy 截断 VARCHAR(MAX) 列中的字符串
- java - 如何在 exoplayer 中更改音频语言名称