开发者

Copy part of string field (in table A) into a new field (table B) connected by a table C

开发者 https://www.devze.com 2023-02-04 00:40 出处:网络
Let\'s say i\'ve in a mysql db: table called \"user\" with \"id_user\" and \"extrafield\" table called \"connection\" with \"id_user\" and \"id_content\"

Let's say i've in a mysql db:

  • table called "user" with "id_user" and "extrafield"
  • table called "connection" with "id_user" and "id_content"
  • table called "content" with "id_content" and "user_content"

Into "user_content" i've multiple fields inside like:

<br />::field1::fieldvalue::/field1::&l开发者_运维知识库t;br />::field2::fieldvalue::/field2::<br />

What's the best query to copy "field2" value from "content" inside "extrafield" in "user" ?

Thank you


SET @user = 1;

UPDATE content
SET content.user_content = replace(content.user_content, 'field2', 
         (SELECT extrafield FROM user WHERE id_user = @user))
WHERE content.id_content 1;

This query will do the replace for you. You have to join the tables appropriately though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号