Common small syntax 2 in postgresql

Source: Internet
Author: User
Example 1. PostgreSQL testing may require loop insertion of N multiple data. At this time, writing a function is troublesome. We can use generate_series For example: mrapp # createtabletest_series (idint)

Postgresql commonly used small syntax 2 postgresql commonly used small syntax http://www.2cto.com/database/201305/210793.html 1. for tests in PG, we may need to insert N multiple data cyclically. In this case, writing a function is troublesome. We can use generate_series For example: mrapp = # create table test_series (id int)

Common small syntax 2 in postgresql

Common small syntax in postgresql

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

1. for tests in PG, we may need to insert N multiple data cyclically. At this time, writing a function is quite troublesome. We can use generate_series.

Example:

Mrapp = # create table test_series (id int );

CREATE TABLE

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

Inserts 0 100000

Mrapp = # select count (1) from test_series;

Count

--------

100000

(1 line record)

2. Compare the number of overlapping strings in postgresql

First, we need to convert array into row data and then deduplicate it with another converted array, and then combine it into an array.

Example:

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

Unnest

--------

1

2

4

(3 rows of Records)

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

Unnest

--------

2

4

(2 rows of Records)

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

, 3, 4]);

Array

-------

{2, 4}

(1 line record)

3. Convert postgresql columns to Arrays

The array_agg function can be used for processing.

Example:

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

A

---

1

2

3

(3 rows of Records)

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 line record)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.