how to copy one column value to another in mysql | copy one column to another in mysql

How to copy one column value to another in mysql

Let’s say the user has not provided a permanent address, but he has provided a communication address. We want to copy one column to another in mysql in our case the same address into the permanent address column of the database.

In another case, we want to copy those user data that doesn’t provide the permanent address.

There is a much easier query to do this in Mysql. In our example, we have a user table with column communication_address and permanent_address.

We can copy the entire column value to another column by the following query.

UPDATE
    `users`
SET
    `permanent_address` = `communication_address`;

Update only that user who hasn’t provided a permanent address.

UPDATE
    `users`
SET
    `permanent_address` = `communication_address`
WHERE
    `permanent_address` = '';

How to copy a column from one table to another in mysql

Suppose we have one more table user_score and we want to copy all user id to that table.

INSERT INTO user_score(user_id)
SELECT
    ID
FROM
    users;
It would copy all users to another table using the above command. 

Leave a Comment

Your email address will not be published. Required fields are marked *