mysql關聯多表進行update更新操做

mysql關聯多表進行update更新操做mysql


mysql關聯多表進行update更新操做sql

UPDATE Track
spa

INNER JOIN MV
ci

ON Track.trkid=MV.mvid
it

SET Track.is_show=MV.is_show WHERE trkid<6 table

等同於
UPDATE Track,MV
擴展

SET Track.is_show=MV.is_show
date

WHERE Track.trkid=MV.mvid and trkid<6scroll

【擴展】 根據結果集進行update更新操做 原表信息 表1:am_favorites_4  im

am_favorites_4

af_user_id

af_tag_id

af_content_id

af_content_type

374

0

535522

3

374

0

535522

3

374

89

535522

3

表2:am_tag_user_4

am_tag_user_4

atu_user_id

atu_tag_id

atu_num

374

0

9

374

89

9


更新結果目標表:am_tag_user_4  

am_tag_user_4

atu_user_id

atu_tag_id

atu_num

374

0

7

374

89

8

UPDATE am_tag_user_4 tag

INNER JOIN am_favorites_4 fav

ON tag.atu_tag_id=fav.af_tag_id and tag.atu_user_id=fav.af_user_id

INNER JOIN (SELECT af_user_id,af_tag_id,count(*) as cnt

FROM am_favorites_4,am_tag_user_4

where atu_tag_id=af_tag_id and atu_user_id=af_user_id and af_content_id = 535522 and af_content_type=3 and af_user_id=374

group by af_user_id,af_tag_id) AS T1

ON tag.atu_tag_id=T1.af_tag_id and tag.atu_user_id=T1.af_user_id

SET tag.atu_num=tag.atu_num- T1.cnt

相關文章
相關標籤/搜索