一张表中  要查询年份为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.   

    --> 测试数据:#
    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
    */
      

  2.   

    create table tb(ID int,name varchar(10),[year] int)
    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 行)*/
      

  3.   

    create  table #temp(ID int, name varchar(8), year int)insert into #tempselect 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', 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 行受影响)
      

  4.   


    select * from t1 where [year] = 2008 or charindex('ss',name)>0 
      

  5.   

    create  table #temp(ID int, name varchar(8), year int)insert into #tempselect 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', 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 行受影响)
      

  6.   

    Select * from 表 where(year='2008' or name='ss') 不就可以了。
      

  7.   

    这样不知道符合要求不select * from #temp where name='ss' or year='2008'
      

  8.   

    select * from [table] where (year = '2008' or (name like '%ss%' and year <> '2008'))
      

  9.   

    where(year=2008 or name='ss')
      

  10.   


    select * from #tb where year = 2008 or name = 'ss'
      

  11.   

    select * from tb where year = 2008 or name = 'ss'
      

  12.   


    select * from tb
    where 
    year = '2008'
    or
    name = 'ss'
      

  13.   

    select * from tb
    where 
    year = '2008'
    or
    (name like  '%ss%' and year<>'2008')
      

  14.   

    SELECT * FROM tb
    WHERE name='ss' OR YEAR=2008