type = 1
親=行のIDと行の合計ですべてを更新するSQLを作成する正しい方法は何ですかtype=1
。
簡単に言えば、likedsedセットの合計を更新=すべての合計の合計(親=行のID(タイプ= 1))
"id" "type" "parent" "country" "totals"
"3" "1" "1" "US" "6"
"4" "2" "3" "US" "6"
"5" "3" "3" "US" "5"
望ましい結果
"id" "type" "parent" "country" "totals"
"3" "1" "1" "US" "17" ->6+6+5=17
"4" "2" "3" "US" "6"
"5" "3" "3" "US" "5"
私は試してみました(そして失敗しました)
UPDATE likesd a
INNER JOIN (
SELECT parent, sum(totals) totalsNew
FROM likesd
WHERE b.parent = a.id
GROUP BY parent
) b ON a.id = b.parent
SET a.totals = b.totalsNew;
これは、MySQLリファレンスマニュアルで説明されている複数テーブルの構文を使用して行うことができます。
update likesd a, (select parent, sum(totals) as tsum
from likesd group by parent) b
set a.totals = a.totals + b.tsum
where a.type = 1 and b.parent = a.id;
クエリは1つの行を更新し、結果は次のようになります。
+------+------+--------+---------+--------+
| id | type | parent | country | totals |
+------+------+--------+---------+--------+
| 3 | 1 | 1 | US | 17 |
| 4 | 2 | 3 | US | 6 |
| 5 | 3 | 3 | US | 5 |
+------+------+--------+---------+--------+
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加