名字 | 描述 |
---|---|
bigint | 有符号 8 字节整数 |
bigserial | 自增八字节整数 |
bit [ (n) ] | 定长位串 |
bit varying [ (n) ] | 变长位串 |
boolean | 逻辑布尔量 (真/假) |
box | 平面中的长方形 |
bytea | 二进制数据(“字节数组”) |
character varying [ (n) ] | 变长字符串 |
character [ (n) ] | 定长字符串 |
cidr | IPv4 或者 IPv6 网络地址 |
circle | 平面中的圆 |
date | 日历日期(年,月,日) |
double precision | 双精度浮点数字 |
inet | IPv4 或者 IPv6 网络地址 |
integer | 四字节长有符号整数 |
interval [ § ] | 时间间隔 |
line | 平面中的无限长直线 |
lseg | 平面中的线段 |
macaddr | MAC 地址 |
numeric [ (p, s) ] | 可选精度的准确数字 |
path | 平面中的几何路径 |
point | 平面中的点 |
polygon | 平面中的封闭几何路径 |
real | 单精度浮点数 |
smallint | 有符号两字节整数 |
serial | 自增四字节整数 |
text | 变长字符串 |
time [ § ] [ without time zone ] | 一天里的时间 |
time [ § ] with time zone | 一天里的时间,包括时区 |
timestamp [ § ] [ without time zone ] | 日期和时间 |
timestamp [ § ] with time zone | 日期和时间 |
tsquery | 全文检索查询 |
tsvector | 全文检索文档 |
txid_snapshot | 用户级别事务ID快照 |
uuid | 通用唯一标识符 |
xml | XML数据 |
Oracle | PostgreSQL |
---|---|
Varchar2 | varchar |
number | numeric |
date | timestamp/date/time |
不支持boolean,可通过0/1代替 | 支持boolean |
null | null |
create table "test".EMP
(
empno numeric(4) not null,
ename VARCHAR(10),
job VARCHAR(9),
mgr numeric(4),
hiredate date,
sal numeric(7,2),
comm numeric(7,2),
deptno numeric(2)
);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800.00, null, 20);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600.00, 300.00, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250.00, 500.00, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975.00, null, 20);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250.00, 1400.00, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850.00, null, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450.00, null, 10);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000.00, null, 10);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500.00, 0.00, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100.00, null, 20);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950.00, null, 30);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into "test".emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300.00, null, 10);
进行空值处理,类似oracle中nvl函数
select empno, ename, coalesce(comm,0) from "test".emp
字符串聚合,类似oracle中list_agg
Select deptno,string_agg(ename,’,’) fromemp group by deptno;
pg中没有decode函数只能用case…when代替,差评!
select ename, (case job when 'PRESIDENT' then '总裁' else '打工仔' end) as "职位" from "test".emp
Oracle的jdbc连接字符串:jdbc:oracle:thin:@192.168.1.1:1521:ORCL
Postgresql的连接字符串:jdbc:postgresql:@192.168.1.1:5432/database
改写后:
select * from (select * from "test".emp where deptno=20) as t
pg中没有rownum伪列,使用row_number()
select empno, row_number() OVER (ORDER BY empno) as "rownum" from "test".emp;
SELECT to_char(12345, '9999999999999999999')//结果' 12345',结果字符串前面有空格,位数跟格式化模式中9的位数有关;
SELECT to_char(12345, '99999')//结果'12345'
SELECT to_char(12345, '9999')//结果‘####’,当模式串小于数字个数时,字符串会显示为#,位数跟 格式化模式中9的位数有关;
SELECT to_char(12345, '')//结果''
SELECT to_number('12345', '9999999999999999999')//12345
SELECT to_number('12345', '99999')//12345
SELECT to_number(''||12345, '9999')//1234,由于模式是4位,结果忽略最后一位;
SELECT to_number(' 12345', '9999999999999999999')//12345
SELECT to_number(' ab ,1,2a3,4b5', '9999999999999999999')//12345,会忽略所有字符串中非数字字符
select to_number('12,454.8-', '99G999D9S')
或者:
–把’1234’转成整数
select cast(‘1234’ as integer) ;
select cast(‘1234’ as int) ;
*函数* | *返回类型* | *描述* | *例子* |
---|---|---|---|
to_char(timestamp, text) | text | 把时间戳转换成字串 | to_char(current_timestamp, ‘HH12:MI:SS’) |
to_char(interval, text) | text | 把时间间隔转为字串 | to_char(interval ‘15h 2m 12s’, ‘HH24:MI:SS’) |
to_char(int, text) | text | 把整数转换成字串 | to_char(125, ‘999’) |
to_char(double precision, text) | text | 把实数/双精度数转换成字串 | to_char(125.8::real, ‘999D9’) |
to_char(numeric, text) | text | 把numeric转换成字串 | to_char(-125.8, ‘999D99S’) |
to_date(text, text) | date | 把字串转换成日期 | to_date(‘05 Dec 2000’, ‘DD Mon YYYY’) |
to_timestamp(text, text) | timestamp | 把字串转换成时间戳 | to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’) |
to_timestamp(double) | timestamp | 把UNIX纪元转换成时间戳 | to_timestamp(200120400) |
to_number(text, text) | numeric | 把字串转换成numeric | to_number(‘12,454.8-’, ‘99G999D9S’) |
select to_timestamp('2012-05-01 23:58:59','yyyy-mm-dd hh24:mi:ss')
date_trunc(‘year’,now());//返回当前时间年的第一天>>>2018-01-01 00:00:00
date_trunc(‘month’,now());//返回当前月的第一天>>2018-09-01 00:00:00
date_trunc(‘day’,now()); //返回当前时间的年月日>>2018-09-14 00:00:00
date_trunc(‘second’,now()); //返回当前时间的年月日时分秒>>2018-09-14 13:30:50