Anda dapat melakukannya dengan kolom, atau jumlah kolom. Di bawah ini adalah tes di atas meja saya.
SELECT sum(crc32(email)) as crc from users;
+-------------+
| crc |
+-------------+
| 10679459550 |
+-------------+
select sum(crc32(concat(user_id,first_name,last_name,email,reportingManager))) as crc from users;
+------------+
| crc |
+------------+
| 7196315383 |
+------------+
Sunting
Dalam prosedur tersimpan yang melewati nama database dan nama tabel, di bawah ini akan mengembalikan crc. Ini masuk ke INFORMATION_SCHEMA khusus db untuk mengambil nama kolom untuk Anda, dan menggunakan semua kolom dan nilai tersebut untuk menghasilkan checksum.
Prosedur tersimpan:
drop procedure if exists getTableCRC32;
DELIMITER $$
create procedure getTableCRC32
( dbname varchar(80),
tableName varchar(80)
)
BEGIN
set @sql1="select GROUP_CONCAT(`column_name` SEPARATOR ',') into @colNames";
set @sql1=concat(@sql1," FROM `INFORMATION_SCHEMA`.`COLUMNS`");
set @sql1=concat(@sql1," WHERE `TABLE_SCHEMA`='",dbName,"'");
set @sql1=concat(@sql1," AND `TABLE_NAME`='",tableName,"'");
-- select @sql1;
PREPARE stmt1 FROM @sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
-- select @colNames;
set @sql2=concat( 'select sum(crc32(concat(', @colNames, '))) as crc from ',tableName);
-- select @sql2;
PREPARE stmt2 FROM @sql2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
END
$$
DELIMITER ;
Ujilah:
call getTableCRC32('so_gibberish','users');
+------------+
| crc |
+------------+
| 7196315383 |
+------------+
call getTableCRC32('so_gibberish','fish');
+------------+
| crc |
+------------+
| 3273020843 |
+------------+