在我的PostgreSQL数据库中,我试图根据该表和另一个表中的数据更新一个表中的列。在我的用户表中,我正在尝试通过在我的计划表中的几个月中的几个月(例如new_date = 2022-02-27 + 5个月)中将数据添加到整数列中来更新New_date列。如果这是一个表,我可以添加日期 +间隔'1个月 *月,例如 - 更新用户设置new_date = date + Interval'1个月 *月;
,但是,我是无法弄清楚如何使用用户和计划之间的联接表(具有子查询或常见表表达式)进行类似的更新。
缩写): new_date 将根据日期 + 月份
进行 |
更新 |
用户表 ( |
null |
2022-04-15 |
null |
计划表(缩写)
结合表:select users.date.date,planths.new_date,users.new_date,来自用户的new_date从用户加入unders.plan_id = plans.id
date。 |
月 |
new_date |
2022-05-21 |
5 |
|
2022-04-15 |
1 |
|
结果:
更新用户表:
日期 |
new_date |
2022-05-21 |
2022-10-21 2022-21 |
2022-04-15 |
2022-05-15 |
In my Postgresql database, I am trying to update a column in one table based on data in that table and another table. In my users table, I'm trying to update the new_date column by adding data in a separate date column to an integer column in months from my plans table (e.g. new_date = 2022-02-27 + 5 months); if this was a single table I could add date + interval '1 month' * months, such as something like - UPDATE users SET new_date = date + interval '1 month' * months;
However, I am unable to figure out how to do a similar update using a join table between users and plans, with subqueries or common table expressions.
users table (abbreviated): new_date to be updated based on date + months
date |
new_date |
2022-05-21 |
null |
2022-04-15 |
null |
plans table (abbreviated)
join table: SELECT users.date, plans.months, users.new_date FROM users JOIN plans ON users.plan_id = plans.id
date |
months |
new_date |
2022-05-21 |
5 |
|
2022-04-15 |
1 |
|
Result:
updated users table:
date |
new_date |
2022-05-21 |
2022-10-21 |
2022-04-15 |
2022-05-15 |
发布评论
评论(1)
您可以加入表,并将列的值乘以
月份
Interval'1个月'
在update> update> update
语句中:请参阅 demo demo 。
。
You can join the tables and multiply the value of the column
months
byINTERVAL '1 month'
in theUPDATE
statement:See the demo.