tags:

views:

21

answers:

2

I am having trouble filtering data out a relational table. The query is part of a join, but I am stuck on a basic part.

I need to remove all the results with a certain id if the condition is found.

query similar to:

select * from colors where color != 'red' group by id

id  color
1   red
1   blue
1   blue
2   green
2   blue
3   green
3   orange
4    red
5    white

returns 1,2,3,5

I need it to only return 2,3,5

I am not sure what mysql command to use

+1  A: 
select * from colors group by id having not group_concat(color) LIKE "%red%"

should work (not tested).

But it's weird to have many ids with same value...

MatTheCat
select group_concat(color) as colors group by id having not colors like '%red%'
Crunchline
It's anyway a bad way, I think it should be a join table but I don't know your database schema.
MatTheCat
Or something like that. The multiple ids is wierd, but it is how the data is related from another table.
Crunchline
Yea there is a join in the sql and other where clauses, but I didn't need to get into all that for this question.
Crunchline
+1  A: 
SELECT DISTINCT id
FROM my_table
WHERE id NOT IN (
   SELECT id
   FROM my_table
   WHERE color = ?
)
Adrian Smith