且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在mysql更新查询中查找受影响的字段

更新时间:2023-02-07 18:26:23

好吧,我不确定这是否是***的方法,但是它会起作用.请注意,该语法适用于tsql(SYBASE),但您可以理解.

Well, I'm not sure if this is the best way but it will work. Note that the syntax is for tsql(SYBASE) but you get the idea.

1)在更新记录之前:使用WHERE子句与UPDATE查询相同.请注意,新值"就是您要更新的内容,如果某个字段未更新,则只需发送旧值即可.这将告诉您UPDATE查询将补全的文件.

1) Before updating the record: Use WHERE clause same as your UPDATE query. Note that the 'new value' is what you are going to update, if a field is not being updated then just send the old value. This will tell you the fileds that will be udpated by the UPDATE query.

SELECT CASE WHEN field1 <> 'new value' THEN 'field 1' ELSE '' END,
CASE WHEN field2 <> 'new value' THEN 'field 2' ELSE '' END,
CASE WHEN field3 <> 'new value' THEN 'field 3' ELSE '' END,
CASE WHEN field4 <> 'new value' THEN 'field 4' ELSE '' END,
CASE WHEN field5 <> 'new value' THEN 'field 5' ELSE '' END
FROM table_name t
WHERE t.field1 = "old value"

2)更新记录后:使用WHERE子句与UPDATE查询相同.请注意,旧值"是更新之前存在的值.如果可以通过UPDATE更改记录的键,则可能必须相应地更新WHERE子句.这将为您提供已由UPDATE更改的字段.

2) After updating the record: Use WHERE clause same as your UPDATE query. Note that the 'old value' is what was present before the update. if the key fo the record can be changed by an UPDATE, you might have to update the WHERE clause accordingly. This will give you the fields that have been changed by the UPDATE.

SELECT CASE WHEN field1 <> 'old value' THEN 'field 1' ELSE '' END,
CASE WHEN field2 <> 'old value' THEN 'field 2' ELSE '' END,
CASE WHEN field3 <> 'old value' THEN 'field 3' ELSE '' END,
CASE WHEN field4 <> 'old value' THEN 'field 4' ELSE '' END,
CASE WHEN field5 <> 'old value' THEN 'field 5' ELSE '' END
FROM table_name t
WHERE t.field1 = "old value"