• 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)

  • 相关阅读:
    Allegro绘制PCB流程
    KSImageNamed-Xcode
    UIApplicationsharedApplication的常用使用方法
    javascript中间AJAX
    hdu1845 Jimmy’s Assignment --- 完整匹配
    嵌入式控制系统和计算机系统
    Bean行为破坏之前,
    jsonkit 分解nsarray 时刻 一个错误
    IO 字符流学习
    2013级别C++文章9周(春天的)工程——运算符重载(两)
  • 原文地址:https://www.cnblogs.com/songyuejie/p/4890969.html
Copyright © 2020-2023  润新知