这是一个带有示例架构和数据的SQLFiddle 。
我需要能够找到链接到“item_version”记录的“结构”记录,其中“item_version”记录未链接到它的父“结构”记录链接到的所有合作伙伴(通过“item_version_partner”记录)(通过其曾孙表'structure_version_range_partner')。
随着小提琴中加载的数据,我期望以下内容:
项目版本 1链接到Partner 1和Partner 2 - 它的父级 - Structure 1也是如此,因此它不应该出现在结果中。
项目版本 2仅链接到Partner 2,而其父级(结构 2)同时链接到Partner 1 和 Partner 2 - 所以它应该在结果中。
即结构 2记录应该作为唯一的结果弹出。
我希望这很清楚!
更新 1:这是我使用的架构和测试数据:
CREATE TABLE partner (
id bigint(20) NOT NULL AUTO_INCREMENT,
partner_name varchar(255) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE structure (
id bigint(20) NOT NULL AUTO_INCREMENT,
description varchar(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE structure_version (
id bigint(20) NOT NULL AUTO_INCREMENT,
structure_id bigint(20) NOT NULL,
PRIMARY KEY (id),
KEY fk_st_structure (structure_id),
CONSTRAINT fk_st_structure FOREIGN KEY (structure_id) REFERENCES structure (id)
);
CREATE TABLE structure_version_range (
id bigint(20) NOT NULL AUTO_INCREMENT,
structure_version_id bigint(20) NOT NULL,
PRIMARY KEY (id),
KEY fk_svr_structure_version (structure_version_id),
CONSTRAINT fk_svr_structure_version FOREIGN KEY (structure_version_id) REFERENCES structure_version (id)
);
CREATE TABLE structure_version_range_partner (
id bigint(20) NOT NULL AUTO_INCREMENT,
partner_id bigint(20) NOT NULL,
structure_version_range_id bigint(20) NOT NULL,
PRIMARY KEY (id),
KEY fk_svrp_version_range (structure_version_range_id),
KEY fk_svrp_partner (partner_id),
CONSTRAINT fk_svrp_partner FOREIGN KEY (partner_id) REFERENCES partner (id),
CONSTRAINT fk_svrp_version_range FOREIGN KEY (structure_version_range_id) REFERENCES structure_version_range (id)
);
CREATE TABLE item_version (
id bigint(20) NOT NULL AUTO_INCREMENT,
structure_id bigint(20) NOT NULL,
item_version_name varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY fk_iv_structure (structure_id),
CONSTRAINT fk_iv_structure FOREIGN KEY (structure_id) REFERENCES structure (id)
);
CREATE TABLE item_version_partner (
id bigint(20) NOT NULL AUTO_INCREMENT,
item_version_id bigint(20) NOT NULL,
partner_id bigint(20) NOT NULL,
PRIMARY KEY (id),
KEY fk_ivp_item_version (item_version_id),
KEY fk_ivp_partner (partner_id),
CONSTRAINT fk_ivp_item_version FOREIGN KEY (item_version_id) REFERENCES item_version (id),
CONSTRAINT fk_ivp_partner FOREIGN KEY (partner_id) REFERENCES partner (id)
);
insert into partner values (1, 'Partner 1');
insert into partner values (2, 'Partner 2');
insert into structure values(1, 'Structure 1');
insert into structure values(2, 'Structure 2');
insert into structure_version values(1,1);
insert into structure_version values(2,2);
insert into structure_version_range values(1,1);
insert into structure_version_range values(2,2);
insert into structure_version_range_partner values(1,1,1);
insert into structure_version_range_partner values(2,2,1);
insert into structure_version_range_partner values(3,1,2);
insert into structure_version_range_partner values(4,2,2);
insert into item_version values(1,1,'Item Version 1');
insert into item_version values(2,2,'Item Version 2');
insert into item_version_partner values(1,1,1);
insert into item_version_partner values(2,1,2);
insert into item_version_partner values(3,2,1);