Home > Database > Mysql Tutorial > body text

postgresql中常用小语法2

WBOY
Release: 2016-06-07 14:59:05
Original
1390 people have browsed it

postgresql中常用小语法2 postgresql中常用小语法 http://www.2cto.com/database/201305/210793.html 1. PG 中测试可能需要循环插入N多数据 这时候写function等就比较麻烦 我们可以用 generate_series 来 example: mrapp=# create table test_series(id int)

postgresql中常用小语法2

 

postgresql中常用小语法

http://www.2cto.com/database/201305/210793.html

 

1. PG 中测试可能需要循环插入N多数据 这时候写function等就比较麻烦 我们可以用 generate_series 来

example:

 

mrapp=# create table test_series(id int) ;

CREATE TABLE

mrapp=# insert into test_series(id) select generate_series(1,100000);

INSERT 0 100000

mrapp=# select count(1) from test_series;

 count

--------

 100000

(1 行记录)

 

 

2. postgresql 中比较两个字符串中重叠的数量

  我们先将array转换为行数据 然后和另一个转换过的array进行去重 然后再拼为数组即可

 

example:

 

 

mrapp=# select unnest(array[1,2,4]);

 unnest

--------

      1

      2

      4

(3 行记录)

 

mrapp=# select unnest(array[1,2,4]) intersect select unnest(array[2,3,4]);

 unnest

--------

      2

      4

(2 行记录)

 

mrapp=# select array(select unnest(array[1,2,4]) intersect select unnest(array[2

,3,4]));

 array

-------

 {2,4}

(1 行记录)

 

3. postgresql 列转数组

  可以用array_agg 函数来处理

example :

 

 

mrapp=# select 1 as a union select 2 as a union select 3 as a;

 a

---

 1

 2

 3

(3 行记录)

 

mrapp=# select array_agg(t.a) from (select 1 as a union select 2 as a union sele

ct 3 as a) as t;

 array_agg

-----------

 {1,2,3}

(1 行记录)

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template