PostgreSQL | 常用語句記錄

來源:互聯網
上載者:User

標籤:density   com   面積   擷取   序列   use   postgresq   date   自增   

一、基礎操作postgresql備份:pg_dump -U postgres -d gisportal -F c -f i:\db2.dumppostgresql還原:pg_restore -U postgres -d gisportal i:\db2.dump                          pg_restore -U postgres -d gisportal e:\postgres-20150420.backup 備份單張表:pg_dump -U postgres -F c -b -v -f i:\test.bak -t sqs.test gisportal備份整個資料庫(非模式):pg_dump -U postgres -F c -b -v -f i:\test.bak -t sqs.test gisportal 備份資料庫下的模式:         pg_dump -U postgres -F c -b -v -f i:\test.bak -n sqs gisportal還原單張表:pg_restore -U postgres -W -d gisportal -v "d:\poi.bak" 要執行語句,先執行:cd postgresql安裝目錄bin中pg_dump -U postgres  -F c -f i:\gisportal.dump gisportal(備份整個資料庫,備份單個模式表為另一種寫法) pg_dump -h localhost -U postgres(使用者名稱) 資料庫名(預設時同使用者名稱)  -t table(表名) >/data/dum.sqlpg_dump -h localhost -U postgres gisportal  -t 省界_region >i:\pros.sql 插入另一張表的整列:insert into sqs.sqs_role(username) select username from sqs.sqs_user 插入當天日期date:update sqs.sqs_shopmall set version=current_date; 截取字元:update sqs.sqs_poi2 set prcode=substring(prcode,1,2)  (index從1開始,1保留) Null 字元串處理:update property set memorial_no = btrim(memorial_no, ‘ ‘) where memorial_no like ‘ %‘update property set memorial_no = 2  where memorial_no= btrim(memorial_no, ‘ ‘) 多條件模糊查詢:SELECT * FROM table WHERE column LIKE ‘Text%‘ OR column LIKE ‘Link%‘ OR column LIKE ‘Hello%‘ OR column LIKE ‘%World%‘ 二、空間操作計算面積(根據geometry計算面積) update sqs.sqs_town set totarea=st_area(geography(geom))/1000000計算距離:select name,st_astext(geom) as geomtext, st_distance(Geography(ST_PointFromText(‘POINT(121.43543 31.2399)‘,4326)),Geography(geom)) as distance from cz_basedata_other_l   where ST_Intersects(st_buffer(geography(ST_PointFromText(‘POINT(121.43543 31.2399)‘,4326)),3000),geom) and dlvydate> date ‘2010-01-01‘ 交叉計算:select st_astext(geom) as GeomText ,"County" ,jdlon as Centerx , jdlat as Centery ,( people::DECIMAL/area::DECIMAL*1000000.0 )::INT as Density from vtownwhere ST_Intersects(st_buffer( geography( ST_PointFromText(‘POINT(121.492498 31.229649)‘,4326)),4000),geom) 數群組轉換為geometry(對百度數組也適用):select  name from sqs.sqs_pipeline where ST_Intersects(ST_GeomFromText(‘POLYGON((121.550391 31.048365,121.845897 31.063214,121.734364 30.859081,121.580286 30.940391,121.550391 31.048365))‘,4326),geom)select  name from sqs.sqs_pipeline where ST_Within(geom,ST_PolygonFromText(‘POLYGON((121.550391 31.048365,121.845897 31.063214,121.734364 30.859081,121.580286 30.940391,121.550391 31.048365))‘,4326)) and status=1 根據geom計算x、y:update sqs.sqs_poi set lng=st_x(geom),lat=st_y(geom) 擷取點geometry的點x、y座標:select *, st_x(geom), st_y(geom) from sqs.sqs_store where status=1 and username=‘admin‘ and  ST_Within(geom,ST_GeomFromText(‘POLYGON((121.289092 31.38394,121.333648 31.275857,121.462429 31.333862,121.459267 31.38172,121.379928 31.405393,121.289092 31.38394))‘,4326)) 點轉換為geometry:update sqs.sqs_pipeline set pipelineid=‘pp00000008‘,name=‘admin‘, geom=ST_PointFromText(‘POINT(121.845897 31.063214 )‘,4326),status=1;update sqs.yichang set geom=ST_PointFromText(‘POINT(‘||lng||‘ ‘||lat||‘)‘,4326);update sqs.sqs_pipeline_wfztest set geomta500=ST_PolygonFromText(st_astext(st_buffer(geography(ST_PointFromText(‘POINT(‘||st_x(geom)||‘  ‘||st_y(geom)||‘)‘,4326)), 500 )) ,4326);  String sql = "select st_Area(Geography(st_intersection(a.geom,b.geom)))/st_Area(geography(a.geom)) as per100 "                + "from sqs.sqs_city a, "+tablename +" b "                + "where st_Intersects(a.geom,b.geom) and b."+id+"=‘"+ storeid+ "‘ and b.username=‘"+username +"‘ and b.status=1" ; String sql = "select ceil(totarea) as totarea,st_asgeojson(b.geom) as geometry, ceil(st_distance(Geography(ST_PointFromText(‘POINT(" + x + " " + y +")‘,4326)),Geography(b.geom))) as distance from sqs.sqs_newhouse b "                + "where ST_Intersects(ST_PolygonFromText(‘POLYGON(("+p+"))‘,4326), b.geom) and "                +"dlvydate > ‘2010-1-1‘ "                +  "order by tothh desc " +  "limit 10";    建立一張新表並從另一張已存在的表複製表結構及資料:create table town_20150525  as  select * from vtown_20150526  添加欄位(自增長序列):alter table cz_users add column gender character varying(50) uniquealter table cz_udatarange add column id  serialunique 唯一值     timestamp without time zone DEFAULT now(),時間    yyyy-MM-dd hh:mm:ss 單獨添加列  空間面:select addgeometrycolumn(‘sqs‘,‘sqs_province‘,‘geom‘,4326,‘MULTIPOLYGON‘,2,TRUE) 前幾條資料:LIMIT num  OFFSET startNum  可以實現指定條數開始取幾條 替換replace:update cz_basedata_test2 set cnty=replace(cnty,city,‘‘)//不可為null,否則整列將會變為null sqs.sqs_pineline 等表中的pipelineid、userid賦值方法:update sqs.sqs_pipeline set userid=‘24c19d21-f519-4a49-869d-6455787537a3‘,status=1,pipelineid=‘pp‘||to_char(id, ‘FM00000000‘),name=‘pp‘||to_char(id, ‘FM00000000‘) where userid is nullupdate sqs.sqs_competitor  set storeid=‘cc‘||to_char(id,‘FM00000000‘),userid=‘e27accfb-e5ff-409c-830f-4a67387237ff‘  where userid is nullupdate sqs.sqs_store set userid=‘55bf68a3-adf2-49d6-89f8-aa05f59cbe01‘,username=‘xxx‘,status=1,storeid=to_char(id, ‘FM00000000‘) where userid is null sql語句中+號:update sqs.sqs_streets set geom=ST_PointFromText(‘POINT(‘||lng||‘ ‘||lat||‘)‘,4326) 

PostgreSQL | 常用語句記錄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.