使用MySQL的MD5()函数获取每一行的哈希值。语法如下-
SELECT MD5(CONCAT(yourColumnName1,yourColumnName2,yourColumnName3,.......N)) as anyVariableName FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table getHashValueForEachRow -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> Age int, -> Marks int, -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入记录。查询如下-
mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Larry',24,89); mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('David',26,98); mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Bob',21,67); mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Sam',22,56); mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Mike',25,80);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from getHashValueForEachRow;
以下是输出-
+----+-------+------+-------+ | Id | Name | Age | Marks | +----+-------+------+-------+ | 1 | Larry | 24 | 89 | | 2 | David | 26 | 98 | | 3 | Bob | 21 | 67 | | 4 | Sam | 22 | 56 | | 5 | Mike 25 | 80 | | +----+-------+------+-------+ 5 rows in set (0.00 sec)
以下是获取每行哈希值的查询-
mysql> select md5(concat(Id,Name,Age,Marks)) as HashValueOfEachRow from getHashValueForEachRow;
这是输出-
+----------------------------------+ | HashValueOfEachRow | +----------------------------------+ | a5f6b8e1a701d467cf1b4d141027ca27 | | 6e649522c773b6a6672e625939eb4225 | | 8bd419e9b7e9e014a4dc0596d70e93c8 | | 504cf50194a2a6e3481dfa9b8568b9e6 | | 08716a8dad7105a00c49ea30d278b315 | +----------------------------------+ 5 rows in set (0.00 sec)