PostgreSQL simple select(group by and insert into ...select)

warehouse_db=# create table student(number int primary key,name varchar(20),age int);
CREATE TABLE
warehouse_db=# insert into student values (1,‘David‘,20);
INSERT 0 1
warehouse_db=# insert into student values (2,‘Luna‘,21);
INSERT 0 1
warehouse_db=# insert into student values (3,‘Lisa‘,21);
INSERT 0 1
warehouse_db=# insert into student values (4,‘Susan‘,22);
INSERT 0 1
warehouse_db=# insert into student values (5,‘Jack‘,22);
INSERT 0 1
warehouse_db=# insert into student values (6,‘Peter‘,23);
INSERT 0 1
warehouse_db=# insert into student values (7,‘Andrew‘,23);
INSERT 0 1
warehouse_db=# insert into student values (8,‘Stallman‘,23);
INSERT 0 1

warehouse_db=# select * from student ;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)
分组查询
warehouse_db=# select age,count(*) from student group by age;
age | count
-----+-------
23 | 3
20 | 1
22 | 2
21 | 2
(4 rows)

warehouse_db=# select * from student order by age;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)

warehouse_db=# select * from student order by age desc;
number | name | age
--------+----------+-----
8 | Stallman | 23
6 | Peter | 23
7 | Andrew | 23
5 | Jack | 22
4 | Susan | 22
3 | Lisa | 21
2 | Luna | 21
1 | David | 20
(8 rows)

warehouse_db=# select * from student where age <= 22 order by age desc;
number | name | age
--------+-------+-----
4 | Susan | 22
5 | Jack | 22
2 | Luna | 21
3 | Lisa | 21
1 | David | 20
(5 rows)

warehouse_db=# create table student_bak(number int primary key,name varchar(20),age int);
CREATE TABLE
warehouse_db=# insert into student_bak select * from student;
INSERT 0 8
warehouse_db=# select * from student_bak ;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)

时间: 2025-01-06 01:43:56

PostgreSQL simple select(group by and insert into ...select)的相关文章

select into from和insert into select from两种表复制语句区别

select into from和insert into select from两种表复制语句区别 select * into target_table from source_table; insert into target_table(column1,column2) select column1,5 from source_table; 以上两句都是将源表source_table的记录插入到目标表target_table,但两句又有区别.第一句(select into from)要求目标

select into from 与 insert into select 区别

1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1 要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量.示例如下: INSERT INTO SELECT语句复制表数据 --1.创建测试表 create TABLE Table1 ( a varchar(10), b varc

select into from 和 insert into select 的用法和区别

本文导读:Insert 是T-sql中常用语句,但我们在开发中经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个 table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了 select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建.insert into

select into from 和insert into select的用法和区别

select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建.insert into select from 要求目标表存在 下面分别介绍两者语法 INSERT INTO SELECT语句 语句形式为: Insert into Table2(field1,field2,...) select value1,value2,... from Table1 注意地方: (1)要求

select into from 和 insert into select

select into from 和 insert into select都是用来复制表, 两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建. insert into select from 要求目标表存在   备份表数据: create table emp as select * from scott.emp 还原表数据:insert into emp select * from scott.emp    1. 复制表结构及其数据: create

select into from 与insert into select from区别

select * into target_table from source_table; insert into target_table(col1, col2)select col1, 5 from source_table 相同点: 1.以上两句都是将源表source_table的记录插入到目标表target_table 不同点: 1.(select into from)要求目标表target_table不存在,因为在插入时会自动创建 (insert into select form)要求

SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

SELECT * into xnbData from (select * from xnbXlsData) select * INTO xnbData from xnbXlsData  ------------------------------ Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我们在开发.测试过程中,经常会遇到需要表复制的情况,如

oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解

我们经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了. 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1 注意:(1)要求目标表Table2必须存在,并且字段fie

SELECT INTO 和 INSERT INTO SELECT 两种表复制语句.txt

Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我们在开发.测试过程中,经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了. 1.INSERT INTO SELECT语句 语