je le schéma suivant:Sélectionnez avec propre tableau. 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');
Je dois trouver une course (en fait plus court) qui ont des erreurs ERROR2 TYPE et PANNE « Apparue nouvelle difficulté » et « Paramètres Mismatch ».
Correct
1 -> 'ERROR2 TYPE'
1 -> 'Appeared new difficulty'
1 -> 'Parameters Mismatch'
incorrect
2 -> 'Parameters Mismatch'
2 -> 'Appeared new difficulty'
Ainsi, la demande devrait trouver toutes les pistes où demandé échoue et des erreurs appartiennent à une course particulière:
La course 1
ne convient pas moi car il ne possède pas de 'Parameters Mismatch' et n'a pas l'erreur ERROR2 TYPE. La course 2
me convient car elle contient toutes les erreurs (ERROR2 TYPE) et toutes échouent ('Apparition de nouvelle difficulté' et 'Erreur de paramètres'). Le run 3
me convient car il a toutes les erreurs (ERROR2 TYPE) et tout échoue ('Apparition de nouvelle difficulté' et 'Erreur de paramètres'). Ce n'est pas grave si l'exécution a échoué davantage (elle a le nouvel échec: 'Mémento du journal des pièces jointes') que ce que nous avions demandé mais elle doit avoir au moins demandé des erreurs et des erreurs.
Comment puis-je le faire?
SQL suivante ne fonctionne pas du tout (il ne trouve rien):
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'
Ce code Soens; t travailler aussi, il ne rechercher que des 1 match, mais pas l'ensemble des ('Apparue nouvelle difficulté ',' Incompatibilité des paramètres '):
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
Semble avoir besoin d'une sorte d'intersection.