如何在比较两列与相应表(SQL脚本)的基础上插入值?
我有两个名为user和customer的表 在userTable中,我有如何在比较两列与相应表(SQL脚本)的基础上插入值?,sql,spring,postgresql,Sql,Spring,Postgresql,我有两个名为user和customer的表 在userTable中,我有 id | username | password | role_id ----+--------------+----------------+--------- 1 | sharam | asdfgh | 2 | ghandhi | iiiooo | 3 | amir | Qwertyu
id | username | password | role_id
----+--------------+----------------+---------
1 | sharam | asdfgh |
2 | ghandhi | iiiooo |
3 | amir | Qwertyuiop@1yy | 2
4 | raju | Qwertyuiop@1yy | 2
5 | somu | Qwertyuiop@1yy | 2
6 | guna. | Qwertyuiop@1yy | 2
在我的客户表格里
Name | user_name | password | email | phone_number | user_id
------------+--------------+-----------------+--------------------+--------------+---------
MansiMansi | sharam | Qwertyuiop@1yy | mansi@mansyiy.com | 8992637877 |
MansiMansi | ghandhi | Qwertyuop@1yy | mansi@mansyi.com | 8992637857 |
MansiMansi | amir | Qwertyuop@1yyg | mansi@mansyig.com | 8992537857 |
MansiMansi | raju | Qwertyuhop@1yyg | manshi@mansyig.com | 8692537857 |
这里我想比较customerTable中的用户名和userTable中的用户名,如果两者相同,我需要将userTable中的id插入customerTable中的用户id
现在,结果应该是在执行脚本之后。。。我的客户表应该是
Name | user_name | password | email | phone_number | user_id
------------+--------------+-----------------+--------------------+--------------+---------
MansiMansi | sharam | Qwertyuiop@1yy | mansi@mansyiy.com | 8992637877 | 1
MansiMansi | ghandhi | Qwertyuop@1yy | mansi@mansyi.com | 8992637857 | 2
MansiMansi | amir | Qwertyuop@1yyg | mansi@mansyig.com | 8992537857 | 3
MansiMansi | raju | Qwertyuhop@1yyg | manshi@mansyig.com | 8692537857 | 4
朋友们,我需要SQL脚本来解决这个问题,尤其是POSTGRES脚本。请不要使用**任何连接**概念
插入到已注册的\u客户\u id中,从usertable中选择id,其中username中的username从已注册的\u客户中选择username;我尝试过这个,但它不起作用。在Postgres中,您可以使用:
update customertable c
set user_id = u.id
from usertable u
where u.username = c.username;
您的代码引用了问题中未描述的附加表。Awesome!!!它起作用了,非常感谢我们面临的一个问题是,它在postgres中工作,但在h2数据库中不工作。。。我们正在使用spring boot。。。所以,你能帮助我们使这个脚本在h2和postgress数据库上都工作吗?@NatheeshkumarRangasamy。你应该问一个关于H2的新问题。