简体   繁体   English

如何将文本列值从mySQL中的一行复制到另一行

[英]How to copy text column value from one row to another row in mySQL

I have table pref having column value. 我有表pref有列值。 This value has type text. 该值具有类型文本。 I want copy the value field value of row with id 7 to the value field of row with id 1. Can you please help how to do this. 我想将id为7的行的值字段值复制到id为1的行的值字段。能否请您帮忙完成此操作。 I know MS SQL, but I am new to mySQL. 我知道MS SQL,但我是mySQL的新手。

create table pref
(
   id int,
   value text
)

In MySQL you can't use a subselect from the same table you are updating, but you can use a join. 在MySQL中,您不能使用要更新的同一个表中的子选择,但可以使用连接。

   UPDATE pref AS target
LEFT JOIN pref AS source ON source.id = 7
      SET target.value = source.value
    WHERE target.id = 1;
UPDATE
    pref
SET
    value = (SELECT value WHERE id = 7)
WHERE
    id = 1

In my case, I was trying to copy an encrypted value from one row into an empty field in another row in the same table. 在我的例子中,我试图将一行中的加密值复制到同一个表中另一行的空字段中。 In this case, I needed to copy john's PIN into Jane's PIN. 在这种情况下,我需要将约翰的PIN复制到Jane的PIN中。

@mohang has it right. @mohang说得对。 Here is my adaptation :) 这是我的改编:)

name   pin

john   sdhjduwhdowodw7d87e838g83g8g3of...
jane       

    //copy from field with value into empty field

UPDATE my_table AS target
LEFT JOIN my_table AS source ON source.pin != ""
SET target.pin = source.pin
WHERE target.pin = "";

    // Hurray, it works!

name   pin

john   sdhjduwhdowodw7d87e838g83g8g3of...
jane   sdhjduwhdowodw7d87e838g83g8g3of...   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM