帮助一个SQL查询

问题描述:

我一直在尝试不同的SQL查询的是做到以下几点:帮助一个SQL查询

对于具有已在TABLE_1的clumn_a一个FIELD_VALUE这TABLE_2的column_b等于任何字段值字段中的所有表项将table_2中字段的id插入到table_1的id_of_the_other_entry_column中。

但是,我只能使用SQL,并没有使用任何其他的SQL命令。这甚至可能没有另一种编程语言的帮助?如果是这样..任何提示?

编辑: 一个例子:

TableOwner 
id name 
1 SomeCompany 
2 SomeOtherCompany 

TableContracts 
Name    ownerid 
NewCompany  Null --> Should remain null after the query 
SomeCompany  Null -->Should change to 1 after the query 
SomeCompany  Null -->Should change to 1 after the query 
SomeOtherCompany Null -->Should change to 2 after the query 

感谢

update tableContracts t2 set t2.ownerid=t1.id where exists(select t1.id from tableowner t1 where t1.name=t2.Name) 

UPDATE 
    table_2 
SET 
    id_of_the_other_entry_column = t1.ID 
FROM 
    table_2 t2 
INNER JOIN 
    table_1 t1 
ON 
    t1.column_a = t2.column_b 

编辑:

与MySQL只是尝试这样做,但没有成功。 随着MySQL的使用:

UPDATE 
    table_2 as t2 
SET 
    id_of_the_other_entry_column = (SELECT 
             t1.ID 
            FROM 
             table_1 as t1 
            WHERE 
             t1.col_a = t2.col_b 
            )

Importent: 如果TABLE_1包含超过一个排,table_1.col_a等于table_2.col_b该命令将中止。

+0

但是select会返回一组许多不同的id,因为t1.col_a = t2.col_b对于许多不同的id是真实的。 – woolagaroo 2011-01-19 08:05:54

+0

是table_2链接到许多table_1行? – Wowa 2011-01-19 08:22:00