--string_agg 相当于php里的implode
--array_to_string 把数组字段转成字符串
select string_agg(DISTINCT(array_to_string(app_ids, ',') ),',') as item FROM app_group;
Data type:
smallint (3w, alias: int2), integer (2 billion, alias int4), bigint are all integer types (2 billion, alias int8)
smallint = int2
integer = int =int4
bigint = int8
Characteristic data types: array, inet, json, xml, serial, boolean, UUID type, geometry type
View stored procedure details
SELECT prosrc FROM pg_proc WHERE proname = 'xxxxx';
About self-increase
It seems that you have to build your own sequence, "id" int4 NOT NULL DEFAULT nextval('iot_client_id_seq'::regclass), ---increment, in fact it is id serial NOT NULL
About ip
"ip" inet, - using these data types to store network addresses is better than using plain text types, because these types provide input error checking and special operations and functions
Newly added array type:
INSERT INTO "Students" VALUES ('colin', '{"音乐", "电影", "读书"}') -- 原生
INSERT INTO "Students" VALUES ('colin', ARRAY['音乐', '电影', '读书'] ) -- 构造函数,只能单引号
Array other operations
访问:select interest[1] from "Students" where id = 1 -- 不能加引号
取多个:select interest[1:2] from "Students" where id = 1
搜索:select "interest" from "Students" where '睡觉' = ANY("interest");
展开数组:select unnest("interest") from "Students" where '睡觉' = ANY("interest");
@> ==== contains
&& ==== 取交集
|| ==== 取并集 但要注意 数组与数组,元素与数组、数组与元素的情况
特点:1、可以直接访问元素;2、
坑:
注意1:双引号单引号这里不能混用:'{"音乐", "电影", "读书"}'
注意2:插入后在数据库查看是{音乐,电影,读书} ,而不是{'音乐','电影','读书'}
1、数组的形式不是 [] 包裹而是 {}
2、数组的索引不是从 0 开始而是从 1 开始
Pit:
1. The table name, field name, alias, etc. are size-sensitive and will be converted to lowercase by default.
2. If you need to change the created table field to self-increment, you cannot use the serial type (some client software has bugs, This type is provided, but the change will report an error)
3. The functions and stored procedures in
postgresql are one thing. 4. In postgresql, in addition to column names, table names, etc., which are used to identify object names, try not to use double quotation marks, which will be interpreted as column names. . If the field is a keyword, add double quotes.
5. Do not use the query update statement commonly used in Oracle. If update tt set (c1,c2) = (select c1,c2 from ..), you must use the update from new statement. update tt set c1 = t2.c1 ,c2 = t1.c2 from t1 (or subquery)
6. When querying data, NULL values may cause some problems, because an unknown value is compared with any other value, and the result is always Is unknown
Features of json type : 1. Elements can be specified in select and where.
Scenario:
Performance:
Pit:
Advantages over text: 1. Guaranteed to be usable json; 2.
Special wording of available related json functions :
select NULLIF(ip_addr->0->> 'address','')::inet as ip from - Type conversion? ?
gid = ANY(
select array_to_json(array_agg(row_to_json(t)))
Example: select array_to_json(array_agg(row_to_json(t))) from (select id, name from iot_serv_group) t;
Result: [{"id":6," name":"1111555511"},{"id":7,"name":"aa"}]
function
encapsulates the result set into JSON
row_to_json: each row is json
{"id":6,"name":" 1111555511"
array_agg: becomes an array
{"{\"id\":6,\"name\":\"1111555511\"}","{\"id\":7,\"name\":\"aa \"}"}
array_to_json: Array to json
[{"id":6,"name":"1111555511"},{"id":7,"name":"aa"}]
json_extract_path_text - ignore json error
select array_to_json(array_agg(row_to_json(t)))
type conversion: as integer
coalesce: handle null values, from left to right, if one is not null, return
ifnull: determine whether it is null
CAST: type conversion
CAST(coalesce(iar.count_gid, 0) as integer): converted to int,
parse the json
operator to the default value of 0 ->Return JSON object field
operator by key ->> Return JSON object field by text
Example: select unnest(visit_group_ids) from iot_node where mid=1111111;
Result:
11111111
22222222 What is the
difference and scenario between json and array?
What is oid? Is there any oid? Rule summary: no quotation marks: built-in function, when the key name is an array and the index value is taken, single quotation mark: value, when the key name is an array and the index value is taken, the json field takes the value value ("edu_experience"->'name') Double quotes: table name, key name, value nested in value
When inserting conflicts:
PostgreSQL versions before 9.5 can implement functions similar to UPSERT through functions or with syntax.
insert into ke_topic_rank values ('test1','test2','test3', 7) on conflict (cluster,topic,tkey) do update set tvalue=excluded.tvalue;
INSERT INTO test.upsert_test(id, "name")
VALUES (1,'m'),(2,'n'),(4,'c')
ON conflict(id) DO NOTHING;