Copy values from one column to another in the same table

前端 未结 7 1612
别那么骄傲
别那么骄傲 2020-12-02 05:08

How can I make a copy values from one column to another? I have:

Database name: list
number | test
123456 | somedata
123486 | somedata1
232344 | 34


        
相关标签:
7条回答
  • 2020-12-02 05:21

    try this:

    update `list`
    set `test` = `number`
    
    0 讨论(0)
  • 2020-12-02 05:26

    you can do it with Procedure also so i have a procedure for this

     DELIMITER $$
     CREATE PROCEDURE copyTo()
           BEGIN
                   DECLARE x  INT;
                DECLARE str varchar(45);
                  SET x = 1;
                set str = '';
                  WHILE x < 5 DO
                    set  str = (select source_col from emp where id=x);
                update emp set target_col =str where id=x;      
                SET  x = x + 1;
                    END WHILE;
    
           END$$
       DELIMITER ;
    
    0 讨论(0)
  • 2020-12-02 05:28

    Short answer for the code in question is:

    UPDATE `table` SET test=number
    

    Here table is the table name and it's surrounded by grave accent (aka back-ticks `) as this is MySQL convention to escape keywords (and TABLE is a keyword in that case).

    BEWARE, that this is pretty dangerous query which will wipe everything in column test in every row of your table replacing it by the number (regardless of it's value)

    It is more common to use WHERE clause to limit your query to only specific set of rows:

    UPDATE `products` SET `in_stock` = true WHERE `supplier_id` = 10
    
    0 讨论(0)
  • 2020-12-02 05:35

    try following:

    UPDATE `list` SET `test` = `number` 
    

    it creates copy of all values from "number" and paste it to "test"

    0 讨论(0)
  • 2020-12-02 05:35

    Following worked for me..

    1. Ensure you are not using Safe-mode in your query editor application. If you are, disable it!
    2. Then run following sql command

    for a table say, 'test_update_cmd', source value column col2, target value column col1 and condition column col3: -

    UPDATE  test_update_cmd SET col1=col2 WHERE col3='value';
    

    Good Luck!

    0 讨论(0)
  • 2020-12-02 05:39
    UPDATE `table_name` SET `test` = `number`
    

    You can also do any mathematical changes in the process or use MySQL functions to modify the values.

    0 讨论(0)
提交回复
热议问题