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

发布于 2024-10-13 07:34:17 字数 177 浏览 3 评论 0原文

我有具有列值的表首选项。该值具有文本类型。我想将 id 为 7 的行的值字段值复制到 id 为 1 的行的值字段。您能帮助一下如何执行此操作吗?我了解 MS SQL,但对 mySQL 还很陌生。

create table pref
(
   id int,
   value text
)

I have table pref having column value. 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. I know MS SQL, but I am new to mySQL.

create table pref
(
   id int,
   value text
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-10-20 07:34:17

在 MySQL 中,您不能使用正在更新的同一个表中的子查询,但可以使用联接。

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

In MySQL you can't use a subselect from the same table you are updating, but you can use a join.

   UPDATE pref AS target
LEFT JOIN pref AS source ON source.id = 7
      SET target.value = source.value
    WHERE target.id = 1;
月牙弯弯 2024-10-20 07:34:17
UPDATE
    pref
SET
    value = (SELECT value WHERE id = 7)
WHERE
    id = 1
UPDATE
    pref
SET
    value = (SELECT value WHERE id = 7)
WHERE
    id = 1
晌融 2024-10-20 07:34:17

就我而言,我试图将加密值从一行复制到同一表中另一行的空字段中。在本例中,我需要将约翰的 PIN 码复制到简的 PIN 码中。

@mohang 说得对。这是我的改编:)

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...   

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.

@mohang has it right. 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...   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文