2017-07-01 24 views
1
FROM TO  TYPE 
---- ---- ---- 
John Jane Like 
Jill Jane Like 
Jane John Hide 

取消我怎么会写SELECT语句是这样的:除非值选择行由另一行

SELECT * FROM table WHERE to='Jane' UNLESS from='Jane' AND type='hide' 

,使得在上表中,它将返回约翰·吉尔,减去约翰因为简选择隐藏约翰,这意味着只有吉尔会被退回。

回答

3

使用not exists

select t.* 
from t 
where t.to = 'Jane' and 
     not exists (select 1 
        from t t2 
        where t2.from = t.to and t2.type = 'Hide' 
       ); 
+0

我想你的意思't2.to = t.from' –