日期:2014-05-18 浏览次数:20520 次
where (Field='a' and Field2>'2012-2-1') or (Field1='c' and Fied3 between '2012-1-1' and '2012-5-1') or (Field2>'2012-1-1')
------解决方案--------------------
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba') BEGIN DROP TABLE tba END GO CREATE TABLE tba ( Field1 VARCHAR(10), Field2 VARCHAR(10) ) GO INSERT INTO tba SELECT 'A', '2012-01-01' UNION SELECT 'B', '2012-02-01' UNION SELECT 'C', '2012-03-01' UNION SELECT 'D', '2012-04-01' GO select * from tba where Field2 BETWEEN CASE when Field1 ='A' then '2012-02-01' else '2012-01-01' end AND CASE WHEN Field1 = 'C' THEN '2012-05-01' ELSE Field2 END /* Field1 Field2 B 2012-02-01 C 2012-03-01 D 2012-04-01 */
------解决方案--------------------
select * from [table] where Field='a' and Field2>'2012-2-1' union all select * from [table] where Field1='c' and Fied3 between '2012-1-1' and '2012-5-1' union all select * from [table] where Field2>'2012-1-1'
------解决方案--------------------
case when 可以用在 SELECT 、WHERE 、ORDER BY 后面
------解决方案--------------------
select * from [table] where Field='a' and Field2>'2012-2-1' union all select * from [table] where Field1='c' and Fied3 between '2012-1-1' and '2012-5-1' union all select * from [table] where Field2>'2012-1-1' and Field!='a' and Field!='c'
------解决方案--------------------