我有以下模式:選擇自己的數組。 PostgreSQL的9.5
create table reports (id bigserial primary key);
create table scens (id bigserial primary key,report_id bigint references reports(id), path_id bigint);
create table runs (id bigserial primary key,scen_id bigint references scens(id));
create table fails (
id bigserial primary key,
run_id bigint references runs(id),
type varchar not null
);
create table errors (
id bigserial primary key,
run_id bigint references runs(id),
type varchar not null
);
INSERT INTO reports(id)
VALUES (1);
INSERT INTO reports(id)
VALUES (2);
INSERT INTO scens(id, report_id)
VALUES (555, 1);
INSERT INTO scens(id, report_id)
VALUES (666, 2);
INSERT INTO runs(id, scen_id)
VALUES (1, 555);
INSERT INTO runs(id, scen_id)
VALUES (2, 666);
INSERT INTO runs(id, scen_id)
VALUES (3, 666);
INSERT INTO runs(id, scen_id)
VALUES (4, 666);
INSERT INTO errors(id, run_id, type)
VALUES (DEFAULT, 2, 'ERROR2 TYPE');
INSERT INTO errors(id, run_id, type)
VALUES (DEFAULT, 3, 'ERROR2 TYPE');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 1, 'Attachment Journal Mismatch');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 2, 'Appeared new difficulty');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 2, 'Parameters Mismatch');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 3, 'Attachment Journal Mismatch');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 3, 'Appeared new difficulty');
INSERT INTO fails(id, run_id, type)
VALUES (DEFAULT, 3, 'Parameters Mismatch');
我需要找到一個運行(實際上更多的跑步),其中有錯誤ERROR2 TYPE和失敗「出現了新的困難」和「參數不匹配」。
正確
1 -> 'ERROR2 TYPE'
1 -> 'Appeared new difficulty'
1 -> 'Parameters Mismatch'
錯誤
2 -> 'Parameters Mismatch'
2 -> 'Appeared new difficulty'
因此,請求應該找哪裏都要求運行失敗和錯誤屬於特定運行:
的1
運行不適合我因爲它沒有'參數不匹配'並且沒有錯誤ERROR2 TYPE。 2
運行適合我,因爲它具有所有錯誤(ERROR2 TYPE)並且全部失敗('顯示新的難度'和'參數不匹配')。 3
運行適合我,因爲它具有所有錯誤(ERROR2 TYPE)並且全部失敗('顯示新的難度'和'參數不匹配')。如果運行失敗(它有新的失敗:'Attachment Journal Mismatch'),那麼運行就沒有問題,但它至少要求失敗和錯誤。
我該怎麼辦?
下列SQL完全不工作(它覺得沒有什麼):
select scens.id as scen_id,
scens.path_id, fails.type, reports.id, runs.id
from reports
inner join scens on reports.id=scens.report_id
inner join runs on runs.scen_id=scens.id
inner join fails on fails.runs_id=runs.id
where reports.id=2 and fails.type=''Parameters Mismatch'
and fails.type='Appeared new difficulty'
此代碼soens;噸工作過,只能搜索1的比賽,但不是一整套(「出現了新的困難','Parameters Mismatch'):
select scens.custom_id as scen_custom_id, scens.id as scen_id,
scens.path_id, scens.category, fails_map.type, f.error_type
from reports
inner join scens on reports.id=scens.report_id
inner join runs on runs.scen_id=scens.id
inner join fails on fails.runs_id=runs.id
INNER JOIN
unnest(array['Appeared new difficulty', 'Parameters Mismatch']) f (error_type)
on fails.type=f.error_type
where reports.id=2
似乎我需要某種交集。