Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Update a full table, inserting a MD5 hash, for each row a specific one

I added a column to an existing table. Now I need to update the tablecontent by adding a MD5 hash to that new column, based on the content of an existing column.

To be more precise:

id | name | date-of-birth | hash
1  | test | 12.12.12      | NULL

There are more than 1 million rows, where hash = NULL. Now I need to update hash with a MD5 string, that is bases on a corresponding column e.g. name: hash = MD5(test)

I know how to do it for a single row. But how to do that for all rows in a single SQL Statement?

like image 658
Ralf Marmorglatt Avatar asked Nov 19 '11 19:11

Ralf Marmorglatt


People also ask

How do I get MD5 value in MySQL?

The MYSQL MD5 function takes any string and returns an MD5 128-bit checksum for that string. MD5 (str); MD5 Hash of an individual row in MySQL To hash each row in a query return, you can concatenate the data into a string and return it as a hash using a combination of the MD5 function and CONCAT function.

How to update data in a mySQL table?

Summary: updating data is one of the most important tasks when you work with the database. In this tutorial, you will learn how to use the MySQL UPDATE statement to update data in a table. The UPDATE statement updates data in a table. It allows you to change the values in one or more columns of a single row or multiple rows.

How to update rows returned by a SELECT statement in MySQL?

Using MySQL UPDATE to update rows returned by a SELECT statement. You can supply the values for the SET clause from a SELECT statement that queries data from other tables. For example, in the customers table, some customers do not have any sale representative.

How to update all entries with MD5 version of name?

Here is the query to update all entries with md5 version of name: mysql> update DemoTable1887 set HashPassword=md5 (Password); Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0


1 Answers

Try this:

UPDATE yourtable
SET hash = MD5(name)
WHERE hash IS NULL

Note that the test is hash IS NULL and not hash = NULL as you wrote in your question.

like image 145
Mark Byers Avatar answered Sep 18 '22 16:09

Mark Byers