MySQL行校验和 [英] MySQL rows checksum
问题描述
是否可以选择表中的整行并获得某种校验和?我正在寻找一种方法,告诉我的代码仅在更改了至少一条记录后才更新数据。从数据更改跟踪的角度来看,这将有助于我减少历史记录表中记录的许多更改。 -谢谢。
Is it possible to select entire row in a table and get sort of checksum? I am looking for a way to tell my code to update data only if at least one record has been changed. From perspective of data changes tracking it would help me to size down a number of changes noted in history table. - Thanks.
推荐答案
您可以结合使用 MD5()
和 CONCAT()
函数生成该行的MD5校验和:
You can combine the MD5()
and CONCAT()
functions to generate an MD5 checksum for the row:
SELECT MD5(CONCAT(col1, col2, col3, ...)) as MD5_checksum FROM table;
如果其中一列可为空,请确保将其包装在 IFNULL中(col,'')
,因为为空将使 CONCAT()
的结果也为空。
If one of the columns is nullable, be sure you wrap it in IFNULL(col, '')
, as a null will make the result of the CONCAT()
also null.
还请注意,这不是100%安全的。如果从一列中删除1个字符并将其添加为下一列的第一个字符,则 CONCAT()
的结果以及MD5哈希的结果将是一样。
Also be aware that this is not 100% safe. If you remove the 1 character from a column and add it as the first character of the next column, the outcome of the CONCAT()
and thus of the MD5 hash will be the same.
这篇关于MySQL行校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!