开发者

Insert Group By count results into a table

How do you insert a group by count result into a table? I'm trying to insert a list of开发者_JAVA百科 names with counts for each.

Thanks!!


You make a select query that gives you the result that you want, then you just put the insert in front of it. Example:

insert into NameCount (Name, Cnt)
select Name, count(*)
from Persons
group by Name


It probably depends on the exact RDBMS you are using, but this syntax is common for the task:

insert into groupTable(name, count) 
    select name, count(*) as count from people 
    group by name

This supposes you already have created the groupTable table. Some engines allow you to create the table directly from the query

create table groupTable as 
    select name, count(*) as count from people 
    group by name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜