select pcbsn, power from caltest where exists (
select sn, pcbsn from snandpcbsn where sn in
(select sn from packlist_br where work_order='1311-14010011')
)
and work_order='1311-13110057'
select pcbsn, power from caltest where work_order='1311-13110057'
我执行上面这两条sql, 得到的结果是一样的. 为什么第一条sql 的EXISTS 为什么不起作用呢?请请各位哪里错了? 数据库是MS SQL 2000
------解决方案--------------------
这个是对的,用的是in。
如果改成exists:
select sn, pcbsn from snandpcbsn where exists
(select 1 from packlist_br where work_order='1311-14010011' and sn = snandpcbsn.sn)
------解决方案--------------------
你的exists貌似没有外层表和内层表关联
------解决方案--------------------
楼主要了解exists的具体含义,exists()的表达式中,如果存在记录,那么条件就是 1 = 1,如果不存在,那么就是 1 = 0,再来看楼主的语句,如果exists的表达式
select sn, pcbsn from snandpcbsn where sn in
(select sn from packlist_br where work_order='1311-14010011')
这个查询存在相应记录,那么这个条件就是 1 = 1,那么查询语句可以看做是
select pcbsn, power from caltest
where 1 = 1 and work_order='1311-13110057'
进一步简化为
select pcbsn, power from caltest
where work_order='1311-13110057'
这个查询是否和第二个查询语句一样了。
------解决方案--------------------
哦,你的是2000,还是2005呢,如果是2005,可以用下面的:
select *
from
(
select *,
ROW_NUMBER() over(partition by pcbsn order by getdate()) rownum
from caltest where exists (
select pcbsn from snandpcbsn where exists
(select sn from packlist_br where work_order='1311-14010011' and sn=snandpcbsn.sn)
and pcbsn=caltest.pcbsn
)
)t
where rownum = 1