zoukankan      html  css  js  c++  java
  • PostgreSQL常用初级技能树

    1.创建表需要id自增

    设置serial即可,示例: id serial not null 

    2.创建表没有设置后面想要再设置自增

    给test表设置一个自增序列test_id_seq

     CREATE SEQUENCE test_id_seq START 10; 

    然后在设计表中添加 nextval('test_id_seq'::regclass) 

    test_id_sql test为表名,start 10,10为索引,从索引开始自增

    3.创建时序表

    CREATE TABLE conditions (
      time        TIMESTAMPTZ       NOT NULL,
      location    TEXT              NOT NULL,
      temperature DOUBLE PRECISION  NULL
    );

    使用 create_hypertable 函数将 postgres 标准表转化为 hypertable

    create_hypertable 有两个参数,第一个参数是表名,第二个参数是分区列,一般为 TIMESTAMPTZ 类型

     SELECT create_hypertable('conditions', 'time'); 

    4.分页查询

    limit后为条数,offset后面为偏移量即索引

     SELECT * FROM test_table limit 100 OFFSET 1000; 

    5.sql中string类型转换为integer类型

    使用cast函数,比如把count string类型转为integer类型

     cast(count as integer) 

    6.sql查询想要查询统计一小时以内的数据

    使用to_char函数,比如按时间time查,统计一小时

     select * from test group by to_char(time, 'yyyy-mm-dd hh24') 

    如果想要按天查就把yyyy-mm-dd hh24改为yyyy-mm-dd,查询分秒亦是如此

    7.获取字符所在位置

    使用position函数,返回目标字符串笫一次出现的位置,如获取字符'-'在name字段的位置

     position('-' in name) 

    也可使用strpos函数,该函数的作用是声明子串的位置,如获取字符'-'在name字段的位置

     strpos(name,'-') 

    8.截取字符串

    使用substring函数,如截取name字段从头到5个字符

     substring(name,0,5) 

    结合第7条,比如截取name字段从头到'-'字符所在位置

     substring(name,0,position('-' in name)) 

    9.连接字段查询显示

    比如想要名称和ip一块显示,如:

    则可以使用字符串连接符||

    所以如上用'-'连接就可以写成 select 名称||'-'||ip from table

  • 相关阅读:
    ArrayList removeRange方法分析
    LinkedHashMap源码分析(基于JDK1.6)
    LinkedList原码分析(基于JDK1.6)
    TreeMap源码分析——深入分析(基于JDK1.6)
    51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划
    51 NOD 1049 最大子段和 动态规划 模板 板子 DP
    51NOD 1006 最长公共子序列 Lcs 动态规划 DP 模板题 板子
    8月20日 训练日记
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/coderxiaobai/p/14149205.html
Copyright © 2011-2022 走看看