views:

23

answers:

1

I am trying to get a list of product id's that do not have certain colors (database is mysql)

Here are my tables:

product
+------------+-------------+
| product_id | description |
+------------+-------------+
|          1 | Widget 1    |
|          2 | Widget 2    |
|          3 | Widget 3    |
|          4 | Widget 4    |
|          5 | Widget 5    |
|          6 | Widget 6    |
+------------+-------------+

color
+----------+-------+
| color_id | name  |
+----------+-------+
|        1 | red   |
|        2 | black |
|        3 | white |
|        4 | green |
|        5 | blue  |
|        6 | pink  |
+----------+-------+

product_color
+------------+----------+
| product_id | color_id |
+------------+----------+
|          1 | 1        |
|          1 | 4        |
|          1 | 5        |
|          2 | 2        |
|          2 | 3        |
|          3 | 1        |
|          3 | 2        |
|          3 | 3        |
|          5 | 3        |
|          5 | 6        |
|          6 | 1        |
|          6 | 5        |
|          6 | 6        |
+------------+----------+

I want to select all the products that don't have colors 4 (green), 5 (blue), or 6 (pink).

So from the tables above, products 2, 3, and 4 would appear in the result set.

The best I've been able to do is:

SELECT product.*, GROUP_CONCAT(product_color.color_id) as color_ids
FROM product
LEFT JOIN product_color USING (product_id)
GROUP BY product.product_id

Here is my result set:

--------------------------------------
product_id  description     color_ids
--------------------------------------
1            Widget 1        1,4,5
2            Widget 2        2,3
3            Widget 3        1,2,3
4            Widget 4        NULL
5            Widget 5        3,6
6            Widget 6        1,3,6

And then I filter them programmatically but I'd prefer for the database to do all the work, if it can.

Optimally, I'd like my result set to look like this (all I need is the product id's).

------------
product_id 
------------
2 
3 
4 

Here is the schema and data just case your kind soul would like to help me out.

CREATE TABLE IF NOT EXISTS `color` (
  `color_id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY  (`color_id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `color` (`color_id`, `name`) VALUES
(2, 'black'),
(5, 'blue'),
(4, 'green'),
(6, 'pink'),
(1, 'red'),
(3, 'white');

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` int(10) unsigned NOT NULL auto_increment,
  `description` varchar(25) NOT NULL,
  PRIMARY KEY  (`product_id`),
  UNIQUE KEY `description` (`description`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `product` (`product_id`, `description`) VALUES
(1, 'Widget 1'),
(2, 'Widget 2'),
(3, 'Widget 3'),
(4, 'Widget 4'),
(5, 'Widget 5'),
(6, 'Widget 6');

CREATE TABLE IF NOT EXISTS `product_color` (
  `product_id` int(10) unsigned NOT NULL,
  `color_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`product_id`,`color_id`),
  KEY `color_id` (`color_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `product_color` (`product_id`, `color_id`) VALUES
(1, 1),
(3, 1),
(6, 1),
(2, 2),
(3, 2),
(2, 3),
(3, 3),
(5, 3),
(1, 4),
(1, 5),
(6, 5),
(5, 6),
(6, 6);
+1  A: 

I think a sub query in the where clause should be sufficient.

SELECT DISTINCT product_id 
FROM product  
WHERE product_id NOT IN (SELECT product_id FROM product_color WHERE color_id IN (4, 5, 6)); 

EDIT: I just saw the many to many bit! This works.

JonVD
That would give me every product_id in the product_color table.
Bill H
Yeah I saw that just after posting, I've reformed the sql to take that into account.
JonVD
You are a saint. Thank you. I wasn't thinking about this problem the right way.
Bill H
Not a problem, never fun to try and figure these out on your own.
JonVD