如何将MySQL数据库student表的password字段批量转为MD5加密?
Fixing Your MD5 Password Encryption Query for MySQL
First off, let's break down why your original query failed: you're mixing SELECT (used to retrieve data) with SET (used to modify data), which isn't valid MySQL syntax. Plus, the @row variable reference doesn't make sense here since your data is already stored in the student table's password field.
Correct Query to Convert Plaintext Passwords to MD5
To update every row in your student table and replace plaintext passwords with their MD5 hashed versions, use an UPDATE statement instead:
UPDATE student SET password = MD5(password);
Handling Extra Whitespace
If your plaintext passwords might have leading/trailing spaces (common when importing CSV files), add TRIM() to clean them up before hashing:
UPDATE student SET password = MD5(TRIM(password));
Quick Notes
- Backup First: Always make a backup of your
studenttable before running mass updates like this—just in case something goes wrong. - MD5 Security: Keep in mind that MD5 is no longer considered secure for password storage. For better security, consider using MySQL's built-in
PASSWORD()function or stronger hashing algorithms like SHA-256 (SHA2(password, 256)).
内容的提问来源于stack exchange,提问作者Alfred Joseph
相关产品推荐
相关产品推荐

