在UPDATE中使用WHERE和SET

时间:2015-09-04 20:47:05

标签: mysql set where

我是MySQL 5.6的新用户并尝试使用NAVICAT更新表 我试图用WHERE来调整SET语句。我似乎采取了错误的做法。有人能指出我正确的方向吗?

UPDATE vacation_watch
INNER JOIN property_names 
ON vacation_watch.Mstrfromproperty = property_names.MstrLink
INNER JOIN property_names_subnames 
ON property_names_subnames.NameLink = property_names.NameLink
SET vacation_watch.Watch_Requester_Last = property_names.Name_Last,
vacation_watch.Watch_Requester_First = property_names.Name_First,
vacation_watch.Watch_Requester_Phone = property_names.Name_Phone,
vacation_watch.Emergency_Contact_Name =  property_names_subnames.Sub_Name_Last WHERE property_names_subnames.Sub_Name_Type = "KEYHOLDER"
vacation_watch.Emergency_Contact_Phone = property_names_subnames.Sub_Name_Phone WHERE property_names_subnames.Sub_Name_Type = "EMERGENCY"

1 个答案:

答案 0 :(得分:0)

您正在以错误的方式使用两个WHERE子句。 您正在寻找的是实际的条件SQL。已经有一个答案可以帮助您实现您想要做的事情here。 此外,只有一个WHERE子句适用于全局级别,而不是在字段级别。

类似的东西:

UPDATE vacation_watch
INNER JOIN property_names 
ON vacation_watch.Mstrfromproperty = property_names.MstrLink
INNER JOIN property_names_subnames 
ON property_names_subnames.NameLink = property_names.NameLink
SET vacation_watch.Watch_Requester_Last = property_names.Name_Last,
vacation_watch.Watch_Requester_First = property_names.Name_First,
vacation_watch.Watch_Requester_Phone = property_names.Name_Phone,
vacation_watch.Emergency_Contact_Name = 
    CASE WHEN property_names_subnames.Sub_Name_Type = "KEYHOLDER"
        THEN property_names_subnames.Sub_Name_Last
    END
vacation_watch.Emergency_Contact_Phone = 
    CASE WHEN property_names_subnames.Sub_Name_Type = "EMERGENCY"
        THEN property_names_subnames.Sub_Name_Phone
    END

可行。我希望我能得到你想要做的正确的,我的语法是正确的,因为我只是在文本编辑器中编写它并没有测试它。