Może najpierw pokaże jaka jest struktura bazy :
CREATE TABLE IF NOT EXISTS `attributes` (
`attribute_ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`attribute_name` text COLLATE utf8_polish_ci NOT NULL,
PRIMARY KEY (`attribute_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=3 ;
INSERT INTO `attributes` (`attribute_ID`, `attribute_name`) VALUES
(1, 'Kolor'),
(2, 'Kształt');
CREATE TABLE IF NOT EXISTS `attributevalues` (
`attributevalue_ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`attributevalue_attribute_ID` int(10) UNSIGNED NOT NULL,
`attributevalue_value` text COLLATE utf8_polish_ci NOT NULL,
PRIMARY KEY (`attributevalue_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=5 ;
INSERT INTO `attributevalues` (`attributevalue_ID`, `attributevalue_attribute_ID`, `attributevalue_value`) VALUES
(1, 1, 'Czarny'),
(2, 1, 'Beżowy'),
(3, 2, 'Prostokąt'),
(4, 2, 'Owal');
CREATE TABLE IF NOT EXISTS `offerattributes` (
`offerattribute_offer_ID` int(10) UNSIGNED NOT NULL,
`offerattribute_attribute_ID` int(10) UNSIGNED NOT NULL,
`offerattribute_attributevalue_ID` int(10) UNSIGNED NOT NULL,
UNIQUE KEY `offerattribute_offer_ID` (`offerattribute_offer_ID`,`offerattribute_attribute_ID`,`offerattribute_attributevalue_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
INSERT INTO `offerattributes` (`offerattribute_offer_ID`, `offerattribute_attribute_ID`, `offerattribute_attributevalue_ID`) VALUES
(1, 1, 1),
(1, 2, 2),
(2, 1, 2),
(2, 2, 4),
(3, 1, 2),
(3, 2, 3),
(4, 1, 2),
(4, 2, 4);
CREATE TABLE IF NOT EXISTS `offers` (
`offer_ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`offer_name` text COLLATE utf8_polish_ci NOT NULL,
PRIMARY KEY (`offer_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=5 ;
INSERT INTO `offers` (`offer_ID`, `offer_name`) VALUES
(1, 'Dywan Shaggy Garlic'),
(2, 'Dywan Hetman'),
(3, 'Dywan Agnus'),
(4, 'Dywan Shaggy Garlic');
chodzi o odpowiednie wyszukanie rekordu Jeśli chcę znaleźć rekord o nazwie 'garlic' koloru beżowego daje takie zapytanie :
SELECT * FROM offers, offerattributes, attributes WHERE offerattribute_offer_ID = offer_ID && offerattribute_attribute_ID = attribute_ID && offerattribute_attribute_ID =1 && offerattribute_attributevalue_ID =2 && offer_name LIKE '%garlic%'
zapytanie zwróci offer_ID = 4.. Jeśli chcę znaleźć rekord o nazwie 'garlic' kształtu owalnego daje takie zapytanie :
SELECT * FROM offers, offerattributes, attributes WHERE offerattribute_offer_ID = offer_ID && offerattribute_attribute_ID = attribute_ID && offerattribute_attribute_ID =2 && offerattribute_attributevalue_ID =4 && offer_name LIKE '%garlic%'
zapytanie zwróci offer_ID = 4..
I teraz pytanie: jak ułozyć zapytanie aby wynikiem był offer_ID = 4 wybierając kształ owalny i kolor beżowy