Sql使用WHEN和WHERE更新NULL值

时间:2014-07-23 07:30:23

标签: php mysql

我无法使用WHEN和WHERE更新NULL行。我的查询有什么问题?

UPDATE `product` SET `city` = CASE `city`   
WHEN NULL THEN 'Gangnam' 
WHEN 'Seouls' THEN 'Seoul'
END  
WHERE city IN (NULL, 'Seoul')

3 个答案:

答案 0 :(得分:4)

IS NULL

比较时,

使用NULL

UPDATE `product` 
SET `city` = 
     CASE    
          WHEN `city` IS NULL THEN 'Gangnam' 
          WHEN `city` = 'Seouls' THEN 'Seoul'
     END  
WHERE city is NULL OR city = 'Seoul'

答案 1 :(得分:1)

UPDATE `product` SET `city` =  
CASE 
    WHEN `city` IS  NULL THEN 'Gangnam' 
    WHEN `city` = 'Seouls' THEN 'Seoul'
END 
WHERE city IS NULL OR city = 'Seouls'

SQL FIDDLE

答案 2 :(得分:0)

在sql中,与NULL的比较总是为假,所以你必须起诉特殊运算符IS NULL

另一种方法是将您的查询分成两部分:

UPDATE product SET city ='Gangnam'WHERE city IS NULL

更新product SET city ='Seouls'WHERE city ='首尔'