Create an aggregate checksum of a column in MySQL



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)
Updated on: 2019-07-30T22:30:25+05:30

625 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements