🏠 ホーム
プログラミング
インフラ
フリーランスCTO
DB
完全無料ツールまとめ
マーケティング

集計用SQL

  DB >  

SQL上で集計用の仮テーブルをgroup byで作成したものをアップデートするSQLを作成しました。

postgresとmysqlで文に違いがありました。

-------mysql---------
update movie_info i,
(
SELECT movie_id, count(*) as com_cnt, delete_flag
FROM movie_comment
where delete_flag = 0
group by movie_id
) s
set i.comment_count = s.com_cnt
where i.movie_id = s.movie_id

-------postgres----------
update quiz q set score_txt = e.end_txt
from
(
select id, 'test' as end_txt from enquete
)
e
where q.enqu_id = e.id
and q.enqu_id > 200

where はSQL前、group byはあとを意識しました。

登録日:

更新日:

by

コメント         tweetでコメント