Wednesday, January 19, 2022

[FIXED] Assign random numbers to a column with NULL values in mySQL

Issue

I have a table named employees and a column named group. Now, upon checking the table, the column has NULL values and I want to assign them a proper numbers from 1 to 10. Can anyone help me on this one? I don't know where to start, please note that i want to update the values that are NULL only.


Solution

Try this update:

UPDATE employees
SET `group` = FLOOR( 1 + RAND( ) *10 )
WHERE `group` IS NULL;

As a side note, GROUP is a MySQL reserved keyword, and you should avoid naming your columns etc. using it.



Answered By - Tim Biegeleisen

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.