日期:2014-05-18  浏览次数:20412 次

如何查询一表排除另一表存在的值
SQL code

表a
id      name    value
5695    白老    25
3286    隐士    26
5814    别处    25
5813    rain    11
5823    rose    7
表b
pid      name 
5695     白老
3286     隐士


想要取得表A中value大于10,且这个id是不存在于表b中的。这个sql命令如何写(a.id同b.pid),我要的结果如下
5814 别处 25
5813 rain 11

------解决方案--------------------
探讨

SQL code
select
*
from a
where not exists(select 1 from b where pid=a.id) and a.value>10

------解决方案--------------------
SQL code
select * from ta
where not exists(select 1 from tb where pid=ta.id) 
      and ta.value>10