MySQL Forums
Forum List  »  Newbie

Re: Compare lookup
Posted by: Peter Brawley
Date: December 03, 2014 04:44PM

I was mistaken, mixing up your problem with another I'm working on. Disregard n-1 and m-1.

There are no unmatched durations if all per-materialID durations in the location table have matches in the material table, and vice-versa.

One way to find the materialIDs that have unmatched durations is to run exclusion joins in both directions ...

select distinct materialID
from (
  select l.materialID
  from location l
  left join material m using(materialID,duration)
  where m.materialID is null
  union
  select m.materialID
  from material m
  left join location l using(materialID,duration)
  where l.materialID is null
) x;

(Look up exclusion joins if the above seems befuddling.)

Then to collect all durations for nonmatching materialIDs, save the above result so it can be joined to a query that fetches the materialIDs ...

drop table if exists t;
create table t
select distinct materialID
from (
  select l.materialID
  from location l
  left join material m using(materialID,duration)
  where m.materialID is null
  union distinct
  select m.materialID
  from material m
  left join location l using(materialID,duration)
  where l.materialID is null
) x;

select data.materialID, group_concat(data.duration)
from (
  select materialID,duration from location
  union
  select materialID,location from material
) as data 
join t using(materialID)
group by data.materialID;

The two steps can be merged into one (cumbersome) query, but it's best to expose logic before efficiencies are sought.

Options: ReplyQuote


Subject
Written By
Posted
December 03, 2014 10:50AM
December 03, 2014 11:05AM
December 03, 2014 12:45PM
December 03, 2014 01:15PM
December 03, 2014 03:24PM
Re: Compare lookup
December 03, 2014 04:44PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.