SQL语句创建表作为计数操作的结果? [英] SQL Statement to create a table as a result of a count operation?

查看:216
本文介绍了SQL语句创建表作为计数操作的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有这样的表格

  id条款
1 a
2 c
3 a
4 b
5 b
6 a
7 a
8 b
9 b
10 b $ b b

,并且您想要这样的报告;

 条款数量
a 4
b 5
c 1
 <$ c $> 

c> SELECT terms,COUNT(id)AS count
FROM table
GROUP BY条款
ORDER BY条件DESC

到目前为止很好。



但上述SQL语句将报表视图放在浏览器上。嗯,我想把这些数据保存到SQL中。



那么,我需要什么SQL命令将该报表的结果插入表格中?



您已经用此创建了一个名为 reports 的表;

 创建表报告(条件varchar(500),count(int))

让我们假设 reports 表为空,我们只想使用以下视图填充它 - 一个单行。我问的问题是如何?

 条款数量
a 4
b 5
c 1


解决方案

很简单:

  INSERT INTO报表
选择条款,COUNT(id)AS count
FROM表
GROUP BY条款
ORDER BY条件DESC


Let's say you got a table like this

id   terms    
1    a       
2    c       
3    a       
4    b       
5    b       
6    a       
7    a
8    b
9    b
10   b        

and you want to end up with a report like this;

terms  count
a      4
b      5
c      1

So you run this over the first table

SELECT terms, COUNT( id) AS count 
    FROM table 
GROUP BY terms 
ORDER BY terms DESC

So far so good.

But the above SQL statment puts the report view on the browser. Well, I want to save that data into a SQL.

So, what SQL command do I need to insert the results of that report into a table?

Assume that you already created a table called reports with this;

create table reports (terms varchar(500), count (int))

Let's assume that the reports table is empty and we just want to populate it with the following view - with a one-liner. The question I'm asking is how?

  terms  count
    a      4
    b      5
    c      1

解决方案

As simple as that:

INSERT INTO reports
SELECT terms, COUNT( id) AS count 
FROM table 
GROUP BY terms 
ORDER BY terms DESC

这篇关于SQL语句创建表作为计数操作的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆