如何在某些条件下更新sql中的列 [英] How to update Column in sql with some condition
本文介绍了如何在某些条件下更新sql中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的桌子上我有一张桌子让我们说学生
这个学生表有列Hobies和主题类
现在我需要做的是
In My Table I have a table Let's say Student
and this student table has columns Hobies and subject class
Now what i need to do is
if(Student.class=2 and Student.Hobies like '%basketball%')
Update Student Set Student.Subject='BasketBall'
if(Student.class=2 and Student.Hobies like '%Football%')
Update Student Set Student.Subject='Football'
但它会抛出错误
But it throw an error
The multi-part identifier "dbo.Student.class" could not be bound.
The multi-part identifier "dbo.Student.Hobies" could not be bound.
有没有人知道它为什么会发生
-Thanks
Does anyone have Idea why it is happening
-Thanks
推荐答案
它会是这样的: -
It will be like this:-
Update Student Set Subject='BasketBall'
where class=2 and Hobies like '%basketball%'
If you have multiple conditions the use it like this
Update Student Set Subject=(CASE WHEN class=2 and Hobies like '%basketball%' THEN 'BasketBall'
WHEN class=2 and Hobies like '%Football%' THEN 'Football'
END)
Update Student Set Student.Subject=
( Case
When Student.class=2 and Student.Hobies like '%basketball%' Then 'BasketBall'
When Student.class=2 and Student.Hobies like '%Football%' Then 'Football' Else Student.Subject END)
这篇关于如何在某些条件下更新sql中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文