Create an aggregate checksum of a column in MySQL

MySQLMySQLi Database

You can use CRC32 checksum for this. The syntax is as follows −

SELECT SUM(CRC32(yourColumnName)) AS anyAliasName FROM yourTableName;

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

mysql> create table CRC32Demo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserId varchar(20)
   -> );
Query OK, 0 rows affected (0.67 sec)

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

mysql> insert into CRC32Demo(UserId) values('USER-1');
Query OK, 1 row affected (0.38 sec)
mysql> insert into CRC32Demo(UserId) values('USER-123');
Query OK, 1 row affected (0.15 sec)
mysql> insert into CRC32Demo(UserId) values('USER-333');
Query OK, 1 row affected (0.13 sec)

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

mysql> select *from CRC32Demo;

Output

+----+----------+
| Id | UserId   |
+----+----------+
|  1 | USER-1   |
|  2 | USER-123 |
|  3 | USER-333 |
+----+----------+
3 rows in set (0.00 sec)

Here is the query to create an aggregate checksum of a column −

mysql> select sum(crc32( UserId)) from CRC32Demo;

Output

+---------------------+
| sum(crc32( UserId)) |
+---------------------+
| 3142885447          |
+---------------------+
1 row in set (0.00 sec)
raja
Published on 19-Mar-2019 06:55:55
Advertisements