gpt4 book ai didi

MySql 使用其父 ID 更新同一表中的数据行

转载 作者:行者123 更新时间:2023-11-29 13:12:04 25 4
gpt4 key购买 nike

在 MySql 中,如何使用其父 ID 更新同一表中的数据行?

在下面的示例中,我尝试使用 id = 1 的路径(因为它是父级)和 的名称值来更新 id = 2 的路径id = 2.

我正在尝试的是完全错误的:

update paths a 
set path = (concat(select path from paths where id = parent,name))
where id = 2;

当前

"id"    "parent"    "path"          "name"
"1" "0" "/us/software/" "microsoft"
"2" "1" "adobe"

预期

"id"    "parent"    "path"                  "name"
"1" "0" "/us/software/" "microsoft"
"2" "1" "/us/software/adobe" "adobe"

从父级获取路径值

最佳答案

UPDATE paths a
JOIN paths b ON a.parent = b.id
SET a.path = CONCAT(b.path, a.name)
WHERE a.id = 2

DEMO

在这种情况下我通常的建议是:阅读 http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

关于MySql 使用其父 ID 更新同一表中的数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com