I have 4 tables;
- articles
- machines
- features
- required features
the problem is that articles can be produced on machines but only on the machines that have the features that are required by the articles.
a product can require 0 or more features and a machine can have 0 or more features
I want to create a query that shows a complet overview of valid article-machine combinations.
the question is: How can I do that?
Below is an example data set for MySQL. it should result in the following query result:
"car";"virtual machine"
"boat";"virtual machine"
"boat";"lean machine"
here’s the dataset:
CREATE TABLE IF NOT EXISTS `articles` (
`name` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `articles` (`name`) VALUES
('car'),
('boat');
CREATE TABLE IF NOT EXISTS `features` (
`machine` text NOT NULL,
`feature_name` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `features` (`machine`, `feature_name`) VALUES
('lean machine', 'punch'),
('virtual machine', 'punch'),
('virtual machine', 'drill');
CREATE TABLE IF NOT EXISTS `machines` (
`name` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `machines` (`name`) VALUES
('lean machine'),
('virtual machine');
CREATE TABLE IF NOT EXISTS `required_features` (
`article` text NOT NULL,
`feature` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `required_features` (`article`, `feature`) VALUES
('car', 'drill'),
('boat', 'punch');
With the data you provide, the solution is just a few joins – but I’m assuming that it’s possible for an article to require more than one feature, and for a machine to have one of those features, but not all the features required.
In this case, you would need to also make sure that a machine that has a matching feature of an article also doesn’t lack any other features required by the article. I don’t think a left join would do the job because it would then return the machine for the matching feature, and the null for the non-matching feature… whereas it shouldn’t return the machine at all in this case.