如何只更新那些可分割的MySQL

如何只更新那些可分割的MySQL,mysql,sql,sql-update,case,Mysql,Sql,Sql Update,Case,它给了我一个错误:错误代码:1048。列“comment”不能为空! 我只想更新那些可分割的。 所有其他不可分割的,我想保持不变。你的“案例”并没有描述每一个可能的案例 13不是0%7、%5、%3或%2 好的做法是添加一个default大小写来捕获这些场景,它在SQL语言中被标记为else。您可以向case表达式添加一个else分支,当它与任何除数都不匹配时,注释保持不变: update comments set comment = case when id % 7 = 0 then '

它给了我一个错误:错误代码:1048。列“comment”不能为空! 我只想更新那些可分割的。 所有其他不可分割的,我想保持不变。

你的“案例”并没有描述每一个可能的案例

13不是0%7、%5、%3或%2


好的做法是添加一个
default
大小写来捕获这些场景,它在SQL语言中被标记为
else

您可以向
case
表达式添加一个
else
分支,当它与任何除数都不匹配时,注释保持不变:

update comments
set comment = case
    when id % 7 = 0 then 'The universe is such an amazing thing.'
    when id % 5 = 0 then 'I definitely read the article again.'
    when id % 3 = 0 then 'This is interesting.'
    when id % 2 = 0 then 'Very good article'
end
where id between 1 and 15;
或者您可以使用
,其中
条件:

update comments
set comment = case
    when id % 7 = 0 then 'The universe is such an amazing thing.'
    when id % 5 = 0 then 'I definitely read the article again.'
    when id % 3 = 0 then 'This is interesting.'
    when id % 2 = 0 then 'Very good article'
    else comment
end
where id between 1 and 15;
如果您确实要过滤1到15之间的值,则可以简化:

update comments
set comment = case
    when id % 7 = 0 then 'The universe is such an amazing thing.'
    when id % 5 = 0 then 'I definitely read the article again.'
    when id % 3 = 0 then 'This is interesting.'
    when id % 2 = 0 then 'Very good article'
end
where 
    id between 1 and 15
    and (id % 2 = 0 or id % 3 = 0 or id % 5 or id % 7 = 0)

id
等于11或13时,子句都不成立,那么
comment
等于什么呢?你要么需要更多的when子句,要么需要一个else子句。这就是我要的。如何让它们保持原样,只改变那些可以整除而没有余数的部分。这是一个考试练习。我当时不理解你的问题(你选择的答案让我理解了你的练习)
 where id between 1 and 15 and id not in (1, 11, 13)