2015-09-28 57 views
0

我有一个表格,例如列id_type和其他列num_area。我想搜索num_area的值与某个值不匹配的所有id_type。SQL:选择该行的某一列与特定值不匹配的行

id_type num_area 
----  ---- 
1   121 
2   121 
1   95 
3   47 
4   65 

对于为例,如果我想ID_TYPE不具备num_area 121,它将返回我,ID_TYPE 3和4

感谢

回答

3

计划

  • list id_type其中num_area是121
  • li ST不同ID_TYPE不是位于上述

查询

select distinct id_type 
from example 
where id_type not in 
(
    select id_type 
    from example 
    where num_area = 121 
) 
; 

输出

+---------+ 
| id_type | 
+---------+ 
|  3 | 
|  4 | 
+---------+ 

sqlfiddle

0

试试这个查询。我得到了没有子查询的结果。

SELECT DISTINCT(e1.id_type) 
FROM example e1 
JOIN example e2 ON(e1.id_type = e2.id_type AND e2.num_area != '121'); 
相关问题