2017-07-01 25 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' –