|
我有这样的查询(在函数中):! {0 n& E6 r" H3 [+ O) R. e
UPDATE some_table SET column_1 = param_1, column_2 = param_2, column_3 = param_3, column_4 = param_4, column_5 = param_5WHERE id = some_id;param_x我的函数参数在哪里?有没有办法不更新这些参数的列?NULL?例如-
$ J; y3 e8 \# b+ m1 w8 o如果param_4和param_5是NULL,然后只更新前三列,离开旧值column_4和column_5。
* a5 g" w2 F" z- [4 M我现在做的是:2 Y! K8 X2 {% F5 _, E
SELECT * INTO temp_row FROM some_table WHERE id = some_id;UPDATE some_table SET column_1 = COALESCE(param_1,temp_row.column_1), column_2 = COALESCE(param_2,temp_row.column_2), column_3 = COALESCE(param_3,temp_row.column_3), column_4 = COALESCE(param_4,temp_row.column_4), column_5 = COALESCE(param_5,temp_row.column_5)WHERE id = some_id;有没有更好的办法?
$ i8 ^( [7 r& h1 {; F* q
. f K6 s7 r, u+ d2 ] 解决方案: ; s* w: S. T5 |9 X8 B! M
删除SELECT句子,不需要使用,只需要使用当前值:8 z1 d% a+ V1 ~3 o2 k
UPDATE some_table SET column_1 = COALESCE(param_1,column_1), column_2 = COALESCE(param_2,column_2), column_3 = COALESCE(param_3,column_3), column_4 = COALESCE(param_4,column_4), column_5 = COALESCE(param_5,column_5)WHERE id = some_id; |
|