I have the following DDLs...
CREATE TABLE IF NOT EXISTS `product` (
`id_product` int(10),
`id_manufacturer` int(10)
);
INSERT INTO `product` (`id_product`, `id_manufacturer`) VALUES
(1,1),
(2,1),
(3,2),
(4,1),
(5,2);
CREATE TABLE IF NOT EXISTS `feature_product` (
`id_feature` int(10),
`id_product` int(10),
`id_feature_value` int(10)
);
INSERT INTO `feature_product` (`id_feature`, `id_product`, `id_feature_value`) VALUES
(5, 1, 9),
(5, 2, 9),
(5, 3, 10),
(5, 4, 10),
(7, 5, 10);
http://sqlfiddle.com/#!2/cbe05/1/0
Can you explain me please, how I can get - all Products with the same Manufacturer and the same Feature_value?
Now (in project) I do it with 2 additional SELECT's (for getting id_manufacturer and id_feature_value), but maybe there are more correct (and fast) way?
Thanks for your time and sorry for my English)
I need too see result like this:
id_product |
-----------|
1 |
2 |
only this 2 products have same manufacturer and (at the same time) same feature value
Just use GROUP_CONCAT:
SELECT GROUP_CONCAT(p.id_product SEPARATOR '\n') AS Products FROM product p INNER JOIN feature_product fp ON (p.id_product = fp.id_product AND fp.id_feature = 5) GROUP BY p.id_manufacturer, fp.id_feature_value HAVING COUNT(p.id_manufacturer) > 1 AND COUNT(fp.id_feature_value)>1;
This will give you the list of Products having multiple Manufacturer Id and Feature Value in a single line, separated by a newline character. You can change the separator as your requirement.
Here is the SQL Fiddle link:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments