日期:2014-05-18 浏览次数:20592 次
DROP TABLE test; CREATE TABLE test( Id INT IDENTITY(1,1), Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 VARCHAR(10), Col4 VARCHAR(10) ); INSERT INTO test(Col1, Col2, Col3, Col4) SELECT 'A01', 'B03', 'C05', 'A01' UNION ALL SELECT 'B04', 'D09', 'N00', 'M30' UNION ALL SELECT 'B01', 'C88', 'B01', 'T10' UNION ALL SELECT 'D05', 'T11', 'N00', 'B05' UNION ALL SELECT 'M33', 'D09', 'N00', 'M33' UNION ALL SELECT 'D09', 'D09', 'D09', 'M30' UNION ALL SELECT 'B04', 'B04', 'B04', 'B04' UNION ALL SELECT 'T55', 'T20', 'Z43', 'Z15' UNION ALL SELECT 'Z78', 'Z34', 'D10', 'D10' UNION ALL SELECT '', '', '', '' UNION ALL SELECT NULL, NULL, NULL, NULL; ---方法一:(最优方法) SELECT * FROM test WHERE (Col1<>'' OR Col2<>'' OR Col3<>'' OR Col4<>'') --排除四个字段均为空字符情况 AND (Col1=Col2 OR Col1=Col3 OR Col1=Col4 OR Col2=Col3 OR Col2=Col4 OR Col3=Col4 ) --方法二:(最差的方法,弱智商方法) SELECT * FROM test WHERE (Col1<>'' OR Col2<>'' OR Col3<>'' OR Col4<>'') --排除四个字段均为空字符情况 AND ( ( Col1=Col2 OR Col1=Col3 OR Col1=Col4 OR Col2=Col3 OR Col2=Col4 OR Col3=Col4 ) --两两相等的情况 OR ( Col1=Col2 AND Col1=Col3 ) --每三个相等的情况 OR ( Col1=Col2 AND Col1=Col4 ) OR ( Col1=Col3 AND Col1=Col4 ) OR ( Col2=Col3 AND Col2=Col4 ) OR ( Col1=Col2 AND Col2=Col3 AND Col3=Col4 ) --四个均相等的情况