select * from pe_A where sc_id='1111' and (enroll_time='2005' and NOT EXISTS (select set3 from sc_setstu where sc_id='1111') b )
解决方案 »
- 在线急等!关于数据同步问题请教
- SSMS 中的DbBackupPlan_1 作业如何删除
- 一个简单的字段相同名称累加的问题,急呀
- 菜鸟求教
- 求教关于SQL的表之间联系的问题!
- 求助,在存储过程中使用临时表的问题。
- 怎么实现只导入数据表的结构.
- 急,在sql server 7.0下如何用语句还原数据库,在线等!!!
- 为什么我的SQL SERVER占用资源这么大?
- 不同服务器上的不同数据库(mssqlserver和oracle)能够进行同步吗?怎样进行?谢谢!
- 在程序中都是通过先连接到数据库后才能执行SQL语句,但是如果数据库还没建立如何调用SQL的查询分析器执行SQL语句?即以下变量还没连接到
- 100分:怎么把几个表合并成一个表啊?
select set3 from sc_setstu where sc_id='1111' and enroll_time='2005'
from pe_A
where sc_id='1111'
OR (enroll_time='2005'
and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
)
select *
from pe_A
where sc_id='1111'
OR (enroll_time='2005'
and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
)
得出的结果是0 (set3是int值)我想要的结果是,当set3=0的时候:
select * from pe_A where sc_id='1111' and enroll_time='2005' 如果它=1就:
select * from pe_A where sc_id='1111'所以起初我这样写;
select * from pe_A where sc_id='1111' and (enroll_time='2005' and NOT EXISTS (select set3 from sc_setstu where sc_id='1111'))但是不对!请问错在哪里啊??
from pe_A
where sc_id='1111'
OR (enroll_time='2005'
and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
)
我按照你们这样,可是也不行啊 。当set3等于0的时候,找出来的纪录enroll_time什么值都有,并没有对 enroll_time进行筛选啊!~~~
我是這樣處理的:
select *
from pe_zong
where sc_id='9437420'
and (enroll_time='2005'
and not EXISTS (select set3 from sc_setstu where sc_id='9437420' and set3=0))是得到我想要的結果了。不過感覺學得比較笨!!!
沒辦法了。我沒有第二選擇
create table Pe_Zong(Sc_id int,Enroll_time int)
insert Pe_Zong select 1,2004
union all select 1,2004
union all select 1,2005
union all select 2,2003
union all select 2,2005
union all select 3,2004
union all select 3,2005
union all select 4,2003create table Sc_SetStu(Sc_id int,set3 int)
insert Sc_SetStu select 1,0
union all select 3,1
go--查询
select a.*
from Pe_Zong a
left join(
select a.Sc_id,Enroll_time=max(a.Enroll_time)
from Pe_Zong a,Sc_SetStu b
where a.Sc_id=b.Sc_id and b.set3=0
group by a.Sc_id
)b on a.Sc_id=b.Sc_id
where b.Sc_id is null or a.Enroll_time=b.Enroll_time
go--删除测试
drop table Pe_Zong,Sc_SetStu/*--测试结果Sc_id Enroll_time
----------- -----------
1 2005
2 2003
2 2005
3 2004
3 2005
4 2003(所影响的行数为 6 行)
--*/