一张表中 要查询年份为2008年的值 但要求这个条件对于name字段包含ss的行无效 包含ss的行要返回例如: ID name year
1 aa 2007
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007查询条件为2008时 返回ID号2,3,4,5希望在Select * from 表 where()括号中解决这个问题
1 aa 2007
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007查询条件为2008时 返回ID号2,3,4,5希望在Select * from 表 where()括号中解决这个问题
if object_id('tempdb.dbo.#') is not null drop table #
create table #(ID int, name varchar(8), year int)
insert into #
select 1, 'aa', 2007 union all
select 2, 'bb', 2008 union all
select 3, 'cc', 2008 union all
select 4, 'ss', 2006 union all
select 5, 'ss', 2007select * from # where year = 2008 or name = 'ss'/*
ID name year
----------- -------- -----------
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007
*/
insert into tb values(1, 'aa', 2007)
insert into tb values(2, 'bb', 2008)
insert into tb values(3, 'cc', 2008)
insert into tb values(4, 'ss', 2006)
insert into tb values(5, 'ss', 2007)
goselect * from tb where [year] = 2008 or name = 'ss'drop table tb/*ID name year
----------- ---------- -----------
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007(所影响的行数为 4 行)*/
select 2, 'bb', 2008 union all
select 3, 'cc', 2008 union all
select 4, 'ss', 2006 union all
select 5, 'ss', 2007
select * from #temp where name='ss' or year='2008'
drop table #tempID name year
----------- -------- -----------
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007(4 行受影响)
select * from t1 where [year] = 2008 or charindex('ss',name)>0
select 2, 'bb', 2008 union all
select 3, 'cc', 2008 union all
select 4, 'ss', 2006 union all
select 5, 'ss', 2007
select * from #temp where year='2008'
union all
select * from #temp where name='ss'ID name year
----------- -------- -----------
2 bb 2008
3 cc 2008
4 ss 2006
5 ss 2007(4 行受影响)
select * from #tb where year = 2008 or name = 'ss'
select * from tb
where
year = '2008'
or
name = 'ss'
where
year = '2008'
or
(name like '%ss%' and year<>'2008')
WHERE name='ss' OR YEAR=2008