我想写一个更新程序
哪种方法最好
1)方法1
if(condition)="P"
update table
set fields1
where field2 = "2" ;
else(condition)="U"
update table
set fields1
where field3 = "3" ;
2)方法2
case condition
when "p"
update table
set fields1
where field2 = "2" ;
when "u"
update table
set fields1
where field3 = "3" ;
我应该使用哪种方法是有理由使用它以及为什么另一种不是一个好的选择。
答案 0 :(得分:2)
update table t
set ...
where (condition='P' and field2='2') or (condition='U' and field3='3')
答案 1 :(得分:0)
我建议你使用 CASE ,因为与 IF 相比, CASE 更有效率和可读性。 但是,如果你有小桌子,只有2个案例要检查..我认为没有任何意义..