常用的postgresql函数

当使geometry报错时继承postgis

create extension postgis

将二进制的geometry格式的转换成  4326的wkt格式

update test3 set geom = st_setsrid(geom,4326)

将划线的方向逆序

update table set geom = st_reverse(geom)

查看字段格式:(返回4326==84坐标系)

select st_srid(geometry) from test_shenz limit 4

创建空间索引

create index idx_test_shenz on test_shenz(geom) using gist 

创建一个表根据查询的结果(返回一个表):

create table longgang_road as 
select a.* from test_longgang as b,test_shenz as a
where st_intersects(b.geom,a.geometry)(查询b包含a里的数据)

将geometry转换成wkt格式:

select st_astext(geometry) from longgang_road limit 3;

将geometry转换成4326坐标系的wkt格式

select st_asgeojson(geom,4326) as wkt from rbeijing limit 3;

创建索引

create index idx_beijing_id on rbeijing(id)

创建自增主键:

alter table "car_group_manage" add column "id" serial primary key;

主键改成 (CP_HM, GPS_SJ)这两个的联合主键:

ALTER TABLE  lh_gps_ls_2019_09

ADD CONSTRAINT pk_lh_gps_ls_2019_09 PRIMARY KEY(CP_HM, GPS_SJ)

新增数据时不新增与联合主键重复的数据:ON conflict (CP_HM, GPS_SJ) DO    NOTHING 

INSERT INTO lh_gps_ls_2019_09 (
  RFID_ID,
 CP_HM,
 WD,
 JD,
 SD,
 GPS_SJ,
 GPS_ZT,
 FX,
 update_time
)
VALUES
 (
   '4dbc0f5e-36a0-478e-9fe6-4bf4b5414c0b',
  '粤BS3869',
  '22.627614019424595',
  '114.04855342603457',
  '0.0',
  '2019-09-09 12:21:11',
  '0',
  '0',
  now()
 ) ON conflict (CP_HM, GPS_SJ) DO
  NOTHING 

数据库中将经纬度转成geometry

st_geomfromtext('Point('||a.jd||' '||a.wd||')',4326)

插入数据返回id

insert into point(pointtype,pointx,pointy,pointval)values(1,2,3,4) RETURNING id;

将查询的结果导成新表

CREATE TABLE films_recent AS
 SELECT * FROM films WHERE date_prod >= '2002-01-01';

关联表更新

update vehicle_manage set own_area_name=qu.id from qu where vehicle_manage.own_area=qu.qname

关联表插入

INSERT INTO car_group_manage_new  (name,own_area_name) SELECT cgroup_name,own_area_name  from vehicle_manage_new  GROUP BY  cgroup_name,own_area_name

删除null数据

delete from 表名 where 字段名 is null;

根据id更新数据,存在更新不存在插入

INSERT INTO t_user (id,username,type,name)  VALUES  ('10','hahh','1','sddddd') ON conflict(id)  

DO  UPDATE  SET  username ='hahh_tst',type='2',name='sddddd_test'

 

 

 

你可能感兴趣的:(PostgreSQL)