Syntax error MySQL get hash value for each row?

MySQL get hash value for each row?



Get hash value of each row using MD5() function from MySQL. The syntax is as follows −

SELECT MD5(CONCAT(yourColumnName1,yourColumnName2,yourColumnName3,.......N)) as anyVariableName FROM yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table getHashValueForEachRow
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Age int,
   -> Marks int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (1.25 sec)

Insert records in the table using insert command. The query is as follows −

mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Larry',24,89);
Query OK, 1 row affected (0.22 sec)

mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('David',26,98);
Query OK, 1 row affected (0.24 sec)

mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Bob',21,67);
Query OK, 1 row affected (0.20 sec)

mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Sam',22,56);
Query OK, 1 row affected (0.22 sec)

mysql> insert into getHashValueForEachRow(Name,Age,Marks) values('Mike',25,80);
Query OK, 1 row affected (0.72 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from getHashValueForEachRow;

The following is the output −

+----+-------+------+-------+
| 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)

The following is the query to get hash value of each row −

mysql> select md5(concat(Id,Name,Age,Marks)) as HashValueOfEachRow from getHashValueForEachRow;

Here is the output −

+----------------------------------+
| HashValueOfEachRow               |
+----------------------------------+
| a5f6b8e1a701d467cf1b4d141027ca27 |
| 6e649522c773b6a6672e625939eb4225 |
| 8bd419e9b7e9e014a4dc0596d70e93c8 |
| 504cf50194a2a6e3481dfa9b8568b9e6 |
| 08716a8dad7105a00c49ea30d278b315 |
+----------------------------------+
5 rows in set (0.00 sec)
Updated on: 2020-06-30T07:24:41+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements