php - how to create a new column contained the hash of id column -
i have table contained 1 column id
. want create new column table, want data of new column hashed of id
. this:
// table +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ // want table +------+------------+ | id | hashed | +------+------------+ | 1 | 00320032 | | 2 | 00330033 | | 3 | 00340034 | +------+------------+
it should noted, hashed
column based:
hash('adler32', '1'); // output: 00320032
hash('adler32', '2'); // output: 00330033
hash('adler32', '3'); // output: 00340034
now, possible ?
among other possible approaches, may first fetch id
s, compute hashed values each 1 of them, , re-insert available date table (and avoid duplicates :)
the following of interest (no error checking done :)
<?php // adding column named 'hashed' $mysqli->query('alter table your_table add column (`hashed` int);'); // retrieving 'id's $result = $mysqli->query('select id your_table;'); $ids = $result->fetch_all(mysqli_assoc); $result->close(); // assembling values bulk insert statement $values = array(); foreach($ids $row) { $id = $row['id']; $hashed = hash('adler32', $id); $values[] = "($id, $hashed)"; } $values = implode(',', $values); // delete avoid duplicates $mysqli->query("delete your_table;"); // insert 'id's , respective hashed values $mysqli->query("insert your_table (id, hashed) values $values;");
Comments
Post a Comment