path
同じテーブルのデータを使用して、テーブル内の hash 列と parType 列を更新しようとしています。ここのSOの回答から次のSQLを作成できますが、更新に失敗したようです。
ここでやろうとしているのは、hash = 0 および parType = 0 のすべての行を、hash = 同じ行 ID および parType = その行の親 ID の型値で更新することです。
私の希望する結果が表示される場合は、理解してください。
手伝ってくれますか?私のSQLを気にしないでください。
SQL
update path t join(
select t1.id, t1.type, t2.parent from path t1, path t2 where t1.parent = t2.id;
) as p on p.id=t.id set hash = t1.id and parType = t1.type;
更新前
"id" "type" "parent" "hash" "parType"
"1" "0" "0" "0" "0"
"2" "2" "1" "0" "0"
"3" "3" "2" "0" "0"
"4" "4" "3" "0" "0"
望ましい結果
"id" "type" "parent" "hash" "parType" //parType = the type of the parent
"1" "0" "0" "1" "0"
"2" "2" "1" "2" "0"
"3" "3" "2" "3" "2" -> This is value from the parents type
"4" "4" "3" "4" "3"
編集 - これは機能しますが、正しい方法かどうかはまだわかりません
update path a join(
select t1.id as hash, t2.type as parType from path t1 inner join path t2 on t1.parent = t2.id
) b on a.id=b.hash set a.hash = b.hash , a.parType = b.parType;