我如何对多列进行数据追加

时间:2014-04-25 10:05:16

标签: mysql

我在表A中添加了一列,现在它是空的。我想要做的是从表consumer获取电话栏,并将其输入到表diabetic的appphone中,只要名字,姓氏,地址,城市,州和邮编匹配即可在两个表中。以下是我一直在尝试的查询,理论上应该有效但不是。无论我改变查询的哪种方式,我都会收到相同的错误.--

错误'子查询返回多行'


UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = (SELECT Consumer.PHONE FROM Consumer
WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)

WHERE EXISTS (SELECT DISTINCT(PHONE) FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)

我跑的原始查询看起来像这样。

UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = Consumer.PHONE
WHERE EXISTS (SELECT * FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

UPDATE Diabetic_DB
    INNER JOIN Consumer ON 
    Consumer.FN = Diabetic_DB.FirstName
    and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
    and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
    and Consumer.ZIP = Diabetic_DB.Zip
SET Diabetic_DB.AppPhone = Consumer.PHONE