from sqlalchemy import * engine = create_engine("postgresql://username:password@localhost/testdb", echo=True)
with engine.connect() as conn: metadata = MetaData() company = Table('company', metadata, autoload_with=engine) ins = company.insert().values(name='阿里巴巴') result = conn.execute(ins) company_id = result.inserted_primary_key[0]
department = Table('department', metadata, autoload_with=engine) ins = department.insert().values(name='研发二部', company_id=company_id) result = conn.execute(ins) department_id = result.inserted_primary_key[0]
employee = Table('employee', metadata, autoload_with=engine) ins = employee.insert().values(name='研发二部', company_id=company_id, department_id=department_id) result = conn.execute(ins) employee_id = result.inserted_primary_key[0]
CREATE TABLE test (path ltree); INSERT INTO test VALUES ('Top'); INSERT INTO test VALUES ('Top.Science'); INSERT INTO test VALUES ('Top.Science.Astronomy'); INSERT INTO test VALUES ('Top.Science.Astronomy.Astrophysics'); INSERT INTO test VALUES ('Top.Science.Astronomy.Cosmology'); INSERT INTO test VALUES ('Top.Hobbies'); INSERT INTO test VALUES ('Top.Hobbies.Amateurs_Astronomy'); INSERT INTO test VALUES ('Top.Collections'); INSERT INTO test VALUES ('Top.Collections.Pictures'); INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy'); INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Stars'); INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Galaxies'); INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Astronauts'); CREATE INDEX path_gist_idx ON test USING GIST (path); CREATE INDEX path_idx ON test USING BTREE (path);
继承关系:
1 2 3 4 5 6 7 8
ltreetest=>SELECT path FROM test WHERE path <@ 'Top.Science'; path ------------------------------------ Top.Science Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology (4rows)
路径匹配:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ltreetest=>SELECT path FROM test WHERE path ~'*.Astronomy.*'; path ----------------------------------------------- Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology Top.Collections.Pictures.Astronomy Top.Collections.Pictures.Astronomy.Stars Top.Collections.Pictures.Astronomy.Galaxies Top.Collections.Pictures.Astronomy.Astronauts (7rows)
ltreetest=>SELECT path FROM test WHERE path ~'*.!pictures@.*.Astronomy.*'; path ------------------------------------ Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology (3rows)
文本搜索:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ltreetest=>SELECT path FROM test WHERE path @ 'Astro*% & !pictures@'; path ------------------------------------ Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology Top.Hobbies.Amateurs_Astronomy (4rows)
ltreetest=>SELECT path FROM test WHERE path @ 'Astro* & !pictures@'; path ------------------------------------ Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology (3rows)
CREATE TABLE company ( id serial NOT NULLPRIMARY KEY, info json NOT NULL );
插入数据:
1 2 3 4 5 6 7
INSERT INTO company (info) VALUES ('{"id":"alibaba","name":"阿里巴巴","departments":[{"id":"research-2","name":"研发二部","employees":[{"id":"2021123","name":"张三"}]}]}'), ('{"id":"alibaba","name":"阿里巴巴","departments":[{"id":"research-2","name":"研发二部","employees":[{"id":"2021124","name":"李四"}]}]}'), ('{"id":"alibaba","name":"阿里巴巴","departments":[{"id":"research-3","name":"研发三部","employees":[{"id":"2021125","name":"王五"}]}]}'), ('{"id":"tencent","name":"腾讯","departments":[{"id":"marketing","name":"市场部","employees":[{"id":"199800001","name":"钱一"}]}]}') ;
查找阿里巴巴的研发二部的所有员工
1 2 3 4 5 6 7 8 9 10 11
SELECT json_array_elements(x.department ->'employees') ->>'id'AS id, json_array_elements(x.department ->'employees') ->>'name'AS name FROM ( SELECT json_array_elements(info ->'departments') AS department FROM company WHERE info ->>'name'='阿里巴巴' ) AS x WHERE department ->>'name'='研发二部' ;
CREATE TYPE employee AS ( id text, name text ); CREATE TYPE department AS ( id text, name text, employees employee array ); CREATE TABLE company ( id text PRIMARY KEY, name text, departments department array );
SELECT (employee).id AS id, (employee).name AS name FROM ( SELECTUNNEST((department).employees) AS employee FROM ( SELECTUNNEST(departments) AS department FROM company ) x WHERE (department).name ='研发二部' ) y ;