2011-01-19 54 views
1

我需要孩子一个需要帮助来编写Sql更新查询?

这个我解释

update table1 set column1 = (select name from table2 where profile = true) 
where column2 in (select id from table2 where profile = true) 

基本上我需要从孩子副本名称和设定值来更新父表是在表1列1,其中ID是在父母相同,子表和表2配置=真

+3

支持多表UPDATE语句在不同的SQL数据库中有很大不同。请说明您需要哪种产品解决方案。 – 2011-01-19 02:24:06

回答

2
update table1 
set column1 = table2.name 
FROM table1 join table2 ON table1.column2 = table2.ID 
where table2.profile = true 

[可能需要调整您的特定SQL方言(未指定RDBMS)]

+0

什么是“档案”?它不应该是'profile ='true'',不是一个字符串吗? – VoodooChild 2011-01-19 02:44:25

+0

@VoodooChild:阅读上面的问题。我不知道海报栏的类型或用法。 – 2011-01-19 02:45:14

1

对于SQL Server,该更新表实际上也是在FROM子句 SQL Server使用位的布尔值(无真/假),所以我怀疑你使用的是别的东西

update table1 
set column1 = table2.name 
from table2 
where table1.column2 = table2.id and table2.profile = 1 

MySQL的形式(和Oracle )

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