Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query to find child record where 2nd child table entries do not match

Tags:

mysql

Here's a SQLFiddle with sample schema and data.

I need to be able to find 'structure' records that are linked to 'item_version' records where an 'item_version' record is not linked to ALL the partners (via 'item_version_partner' records) that it's parent 'structure' record is linked to (via its great grand child table 'structure_version_range_partner').

With the data loaded in the fiddle, I expect the following:

Item Version 1 is linked to both Partner 1 and Partner 2 - and so is its parent - Structure 1, so it should not be in the result.

Item Version 2 is only linked to Partner 2, whilst its parent (Structure 2) is linked to both Partner 1 and Partner 2 - so it should be in the result.

I.e Structure 2 record should pop out as the only result.

I hope that's clear!

UPDATE 1: Here's the schema and test data I used:

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);
like image 212
johan Avatar asked Nov 12 '22 01:11

johan


1 Answers

Based on your update this should be a start.

Select s.Description From Structure s
inner join structure_version sv On sv.structure_id = s.id
inner join Structure_version_range svr on svr.structure_version_id = sv.id
inner join structure_version_range_partner svrp On svrp.structure_version_range_id = svr.structure_version_id
inner join item_version iv on iv.structure_id = s.id
Left join item_version_partner ivp On ivp.item_version_id = iv.ID and ivp.partner_id = svrp.partner_id
Where ivp.partner_id is null

Have a play with it the concept is pretty simple, it's just the normalisation that you've implemented makes it a bit windy. Selecting the partner_ids from the two tables and dropping the where clause will demonstrate how it works.

like image 95
Tony Hopkinson Avatar answered Nov 15 '22 05:11

Tony Hopkinson