Need help to write Sql update query?
I need to update parent table with value from child one
this my explanation
update table1 set column1 = (select name from table2 where profile = true)
where column2 in (select id from table2 where profile = true)
basically i need to copy name from child and set is as col开发者_Python百科umn1 in table1 where ids are the same in parent and child table and profile in table2 = true
update table1
set column1 = table2.name
FROM table1 join table2 ON table1.column2 = table2.ID
where table2.profile = true
[might need tweaking for your particular SQL dialect (RDBMS not specified)]
For SQL Server, the Update table is virtually also in the FROM clause SQL Server uses bit for booleans (no true/false), so I suspect you are using something else
update table1
set column1 = table2.name
from table2
where table1.column2 = table2.id and table2.profile = 1
MySQL form ( and Oracle too )
update table1 a join table2 b on a.column2 = b.id
set column1 = table2.name
where b.profile # 'true = true' is silly
# b.profile = true === just b.profile
精彩评论