我有大小和颜色的表
id option_name
2969 M
2969 Black
2970 S
2970 Blue
2971 S
2971 Black
我需要选择哪些ID相同且option_name等于Black的选项。所以我需要得到结果:
2971 S
2969 M
感谢您的帮助!
答案 0 :(得分:2)
使用子查询检测具有option_name = black的ID 并从主结果中排除option_name = black 例子
select
`table`.`id`,
`table`.`option_name`
from
`table`
where
`table`.`option_name` <> "Black"
and
`table`.`id` in(select `table`.`id` from `table` where `table`.`option_name` = "black")
答案 1 :(得分:2)
尝试以下几点:
select t.id, t.option_name from mytable t
where t.option_name <> 'Black'
and t.id in (select m.id from mytable m where m.option_name = 'Black')