0

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;
4

3 に答える 3

1

このようなもの?

UPDATE table1 t LEFT JOIN
(SELECT t1.id,t1.type FROM table1 t1 INNER JOIN table1 t2 ON t1.id>t2.id GROUP BY t1.id) as p 
ON p.id+1=t.id SET t.hash=t.id,t.parType=ifnull(p.type,0) 

SQLフィドル

于 2013-10-20T15:44:26.550 に答える
0

これで試してください

update path t join(
select t1.id, t2.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 = t2.type;
                                                   ^^

内部結合を からt1.typeに変更しt2.typeON条件を からparType = t1.typeに変更しましたparType = t2.type

于 2013-10-20T15:44:16.313 に答える
0

これを試して

update t set 
   hash = t.id,
   partype = p.type
from path t 
   left join path p
      on p.id = t.parent
where hash = 0 
   and parType = 0
于 2013-10-20T15:46:20.760 に答える