Skip to content

Instantly share code, notes, and snippets.

@limboinf
Created June 2, 2015 05:34
Show Gist options
  • Save limboinf/55187865b13e5289f69a to your computer and use it in GitHub Desktop.
Save limboinf/55187865b13e5289f69a to your computer and use it in GitHub Desktop.
mysql update whit select and group by
-- update table with select and group by statement
-- Ohs my poor SQL.
-- contacts:beginman.cn
update group_info as g
left join (
select gi.group_id, count(gm.id) as nums from group_info gi
inner join group_members gm on gm.group_id = gi.group_id
group by gm.group_id
)
as m on g.group_id = m.group_id
set g.member_count = m.nums;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment