if EXISTS(select * from where 字段1='X' and 字段2='Y')
if exists (select 1 from table where col1='x' and col2='y') print 'true' else print 'false'
if Exists(select 字段2 from Table1 where 字段1='X' and 字段2<>'Y') return false
If Exists (Select * from Table1 where 字段1='X' and 字段2 !='Y') Return false else Return True
if not EXISTS(select * from where 字段1='X' and 字段2<>'Y') begin ........ end
在TABLE1中要求查找满足以下条件的记录: 1/字段1='X' 2/如所有满足条件1的记录中,字段2都是'Y',返回TRUE;字段2有一个不是'Y',返回FALSE用IF EXISTS(),一个SQL语句完成If exists(select * from Table1 where 字段1='X' and 字段2='Y') print 'true' else print 'false'
可能我没把题意说清楚.举几个测试用例: 字段1 字段2 应该返回值 X Y * X Y * TRUE _____1 X Y * --********************************* X Y * X Y * FALSE_____2 X Z * --********************************* Z Y * Z Y * FALSE_____3 Z Y * --********************************* Z Y * Z Y * FALSE_____4 Z Z *If exists(select * from Table1 where 字段1='X' and 字段2='Y') 违反用例2 if not EXISTS(select * from where 字段1='X' and 字段2<>'Y') 违反用例3\4if Exists(select 字段2 from Table1 where 字段1='X' and 字段2<>'Y') return false 符合用例2,逻辑上正确,但是必须再测试用例3\4才能确保所有返回FALSE值的情况都已被考虑实际上,我只想找出用例1的情况,用IF EXISTS(),返回TRUE值即可.说的不对处,请各位指点.
If not exists(select 1 from Table1 where 字段1<>'X' or 字段2<>'Y')
If not exists(select 1 from Table1 where 字段1<>'X' or (字段2<>'Y' and 字段1='X'))
谢谢大力. 1、恕我钻牛角尖,如果一定要用If exists,不能用If not exists,有办法吗? 2、如果用If not exists(select 1 from Table1 where 字段1<>'X' or (字段2<>'Y' and 字段1='X'))的句型: 将原来的条件(字段1='X') 替换为(字段1='X' and 字段3='m' and 字段4='n'),语句怎么写? 要穷举所有的反面条件不成?头都大了!
1、恕我钻牛角尖,如果一定要用If exists,不能用If not exists,有办法吗?If (select count(1) from 表 where 字段1='X')=(select count(1) from 表 where 字段1='X' and 字段2='Y')
if exists ( select * from t where (col1='x' and col2<>'y') or col1<>'x') print 'false' else print 'true'
楼上不行,如: --********************************* X Y * X Y * FALSE_____2 X Z *
If exists(select * from Table1 where 字段1='X' and 字段2<>'Y') begin print 'true' end else begin print 'false' end
如果按这样的思路,能不能写一个语句: 1、做一个子查询,(SELECT * FROM TABLE1 where 字段1='X'); 2、如果这样的子查询存在,检查子查询中是否字段2都是'Y'(NOT EXISTS(字段2<>'Y')?); 3、返回TRUE
not exists (SELECT * FROM TABLE1 where 字段1='X' and 字段2<>'Y')不就可以了?
if exists(select * into #temp from table1 where field1='X' and (exists (select * from #temp where field2<>'Y'))) print 'false' else print 'true'
print 'true'
else
print 'false'
return false
Return false
else
Return True
begin
........
end
1/字段1='X'
2/如所有满足条件1的记录中,字段2都是'Y',返回TRUE;字段2有一个不是'Y',返回FALSE用IF EXISTS(),一个SQL语句完成If exists(select * from Table1 where 字段1='X' and 字段2='Y')
print 'true'
else
print 'false'
字段1 字段2 应该返回值
X Y *
X Y * TRUE _____1
X Y *
--*********************************
X Y *
X Y * FALSE_____2
X Z *
--*********************************
Z Y *
Z Y * FALSE_____3
Z Y *
--*********************************
Z Y *
Z Y * FALSE_____4
Z Z *If exists(select * from Table1 where 字段1='X' and 字段2='Y') 违反用例2
if not EXISTS(select * from where 字段1='X' and 字段2<>'Y') 违反用例3\4if Exists(select 字段2 from Table1 where 字段1='X' and 字段2<>'Y')
return false 符合用例2,逻辑上正确,但是必须再测试用例3\4才能确保所有返回FALSE值的情况都已被考虑实际上,我只想找出用例1的情况,用IF EXISTS(),返回TRUE值即可.说的不对处,请各位指点.
1、恕我钻牛角尖,如果一定要用If exists,不能用If not exists,有办法吗?
2、如果用If not exists(select 1 from Table1 where 字段1<>'X' or (字段2<>'Y' and 字段1='X'))的句型:
将原来的条件(字段1='X') 替换为(字段1='X' and 字段3='m' and 字段4='n'),语句怎么写? 要穷举所有的反面条件不成?头都大了!
print 'false'
else
print 'true'
--*********************************
X Y *
X Y * FALSE_____2
X Z *
begin
print 'true'
end
else
begin
print 'false'
end
1、做一个子查询,(SELECT * FROM TABLE1 where 字段1='X');
2、如果这样的子查询存在,检查子查询中是否字段2都是'Y'(NOT EXISTS(字段2<>'Y')?);
3、返回TRUE
print 'false'
else
print 'true'