postgreSQL
安善良民弱女子
起风了,唯有努力生存!
展开
-
安装postgreSQL
由于最近项目正好要用postgreSQL,而以前一直用的mysql,所以把安装方法总结如下。 我用的是postgresql-9.4.4-3-windows-x64.exe 安装方法如下: 1.点击exe文件,弹出安装向导,如下图: 2.点击next,跳入到安装目录的对话框,选择安装目录,点击next,如下图: 3.跳入给postgres超级管理员输入密码的对话框,如下图: 4原创 2015-10-16 12:28:06 · 528 阅读 · 0 评论 -
pgSQL substring(二):查询字段中除字母之外的字段
源数据:select substring(name,E'([a-zA-Z]+)') from test;结果: 结论:可以看出这个语句只可以截取掉在最前或者最后的数字,或者也可以说,他获取到的是第一次遇到的那一串字母,并把它取出来select substring(name,'%#"[0-9]+#"%','#') from test;结果:结论:他获取到的是最后的那个数字,若要获取字母,则将【0原创 2016-05-11 15:09:50 · 4107 阅读 · 0 评论 -
我和postgreSQL的二三事
今天遇到这段SQL:select itemname,name ,pushdate,mark,queryid,'0' mtype,null departmentName from push where phone = '12345678' union select m.mess_content itemname,m.mess_patient username,m.update_date pushda原创 2015-11-11 15:41:09 · 413 阅读 · 0 评论 -
如何对sql中某个字段里的所有值添加相同的字符
表名:test 字段 test_code有两种方法: 第一种: update test set test_code='00'||test_code where test_name='coco';第二种:update test set test_code='00'+test_code where test_name='coco';总结: 第一种方法在postgreSQL中可以实现!原创 2016-06-06 10:50:40 · 2730 阅读 · 0 评论 -
postgreSQL 自定义array_accum函数
CREATE AGGREGATE array_accum ( sfunc = array_append, basetype = anyelement, stype = anyarray, initcond = '{}');这是一个拼接的聚集函数!原创 2016-06-07 09:20:56 · 1156 阅读 · 0 评论 -
postgresql得到时间对应周的周一
两种方法:第一种:DO $$declare d int;declare d1 varchar(100);declare d2 varchar(100);declare d3 date;declare d4 date;begind3:= CURRENT_DATE;d1:='select date'''|| d3 ||'''';d:=(SELECT EXTRACT(DOW FROM d原创 2016-06-12 18:23:27 · 12977 阅读 · 0 评论 -
pg批量修改字段内容
根据条件获取指定记录update cities set end_time=REPLACE(end_time,end_time,SUBSTRING(end_time,1,5)) where end_time is not null ;原创 2016-09-18 13:37:23 · 3355 阅读 · 0 评论 -
postgres 修改视图
这是视图内容: SELECT *FROM studentsWHERE Sex = 'M';修改视图CREATE OR REPLACE VIEW studentBySexAndClass AS SELECT *FROM studentsWHERE Sex = 'M' and class='一年级';原创 2016-09-20 17:41:08 · 6148 阅读 · 0 评论 -
截图字符串SUBSTRIGN
update students set class_date=REPLACE(class_date,class_date,SUBSTRING(class_date,1,14)) where studentId='7383jskddskldkfjsj' and create_date >'2016-09-19';原创 2016-09-20 17:44:24 · 743 阅读 · 0 评论 -
去除重复语句SQL
表 table1 字段, a ,b,c,d,e查询出那个e字段有多个,并且a字段都为Fselect a,e from table1 group by a,e having a = 'F' and count(e)>1;结果:a eF 55去除重复字段select * from table1 where e in (select e from table1 g原创 2016-09-21 13:53:54 · 1042 阅读 · 0 评论 -
pgSQL substring(一):删除字段中除数字之外的字段
源数据:sql: update test set name=substring(name,E'(\\d+)') where id='2';结果:原创 2016-05-11 14:10:24 · 4621 阅读 · 0 评论 -
postgres查看外键的关联关系
查看city这张表的外键关联关系SELECT tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name, tc.is_deferrable,tc.initial原创 2016-04-05 18:22:30 · 4072 阅读 · 0 评论 -
关于pg的EXTRACT(8:00-10:00)
今天同事忽然问我,怎么获取数据库中的时间段(比如:8:00-10:00),愁死我了,赶紧问朋友问百度,好在朋友告诉了我EXTRACT这个函数! 工具:postgresSQLEXTRACT(field FROM source)比如:select * from news where extract(hour from create_date) between 8 and 10;原创 2015-10-30 14:02:09 · 806 阅读 · 0 评论 -
postgreSQL中的array_to_string函数和如何实现group_concat
工具 postgreSQL9.4今天看到这个SQL语句:select array_to_string(group_concat(department_name),'、') from departments结果: 第一个array_to_string测试数据:create table city(id int,name text);insert into city values(0,'北京'),原创 2015-11-16 14:52:05 · 8559 阅读 · 0 评论 -
postgreSQL中的row_number() 与distinct
select count(s.*)from ( select *, row_number() over (partition by fee_date order by fee_date) as gr from new_order where news_id='novel' and order_status='2' ) swhere s.gr = 1 SELECT coun原创 2015-11-23 15:27:25 · 4261 阅读 · 1 评论 -
postgreSQL中的内连接和外连接
测试数据:city表:create table city(id int,name text);insert into city values(0,'北京'),(1,'西安'),(2,'天津'),(3,'上海'),(4,'哈尔滨'),(5,'西藏')person表:create table person(id int,lastname char(20));insert into person va原创 2015-11-17 10:40:41 · 13136 阅读 · 2 评论 -
pg中replace和translate的用法(数据少的中文排序)
1.首先创建students表CREATE TABLE students( id integer NOT NULL, name character varying(255), sex character varying(255), class character varying(255), "like" character varying(255), school char原创 2016-01-22 16:07:25 · 9967 阅读 · 0 评论 -
pg 查询执行计划
select * from students where sex='女' and school ='第三小学' order by translate(class,'一二三四五六七八九十','0123456789')如下图:查询执行计划: EXPLAIN select * from students where sex='女' and school ='第三小学' order by translate原创 2016-01-26 15:55:16 · 772 阅读 · 0 评论 -
PostgreSQL如何实现MySQL中的group_concat聚集函数(简单的拼接功能)
1.postgreSQL中没有现成的group_concat聚集函数2.postgreSQL可以自定义聚集函数一.首先我们要知道MySQL中的group_concat聚集函数是干什么的? group_concat和group by一起使用 group_concat函数返回一个字符串结果,该结果由分组中的值连接组合而成。例如:SELECT id, group_concat(city) fr原创 2016-03-03 14:08:34 · 21033 阅读 · 7 评论 -
pg查询时间间隔(timestamp类型)
create_date timestamp(6) without time zone 1.从2015-10-12到2015-10-13 之间的4点到9点的数据select * from schedule where create_date between to_date('2015-10-12','yyyy-MM-dd') and to_date('2015-10-13','yyyy-MM-d原创 2016-01-21 18:53:05 · 10605 阅读 · 0 评论 -
postgreSQL中的case
工具:postgreSQL Navicat Premium又一次在看代码的时候,发现了不懂的东西! 这次就是case when then SQL CASE表达式是一种通用的条件表达式,类似于其它语言中的if/else语句。 CASE WHEN condition THEN result [WHEN ...] [ELSE result]原创 2015-12-04 18:02:22 · 8154 阅读 · 0 评论 -
postgresql将表中的字段由varchar类型改为int类型
因业务需要,遂将表中sort_num字段从varchar类型改为int类型,但是在修改中却发现了如下问题: 错误: 字段 “sort_num” 不能自动转换成类型 integer HINT: 指定一个USING表达式来执行转换一.首先我检查了一下sort_num里面的数据,都是数字字符 二.然后使用alter table menus alter column sort_num int4;原创 2017-06-01 17:28:08 · 15861 阅读 · 0 评论