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'
这样,在上面的表格中,由于简选择隐藏约翰,它将返回约翰和吉尔,减去约翰,这意味着只有吉尔会被归还。
答案 0 :(得分: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'
);