SELECT where子句(多列中的多个值)

时间:2016-10-24 13:00:02

标签: sql-server-2008

我有以下select语句。我基本上要做的是选择所有行,其中manager和year等于下面sql中显示的内容,而fcst_jan到fcst_dec列不包含其中一个值。它似乎可以找到一些,但我仍然得到其中一列中包含205的行。所以基本上我想选择任何列jan到dec不包含其中一个值的行。请帮忙。我无法弄清楚。我知道我的桌面设计不是最好的方法,但它就是我所拥有的,所以我需要弄明白。我在SQL Server 2008上。

SELECT [fcst_id]
      ,[fcst_emplname]
      ,[fcst_emplid]
      ,[fcst_posid]
      ,[fcst_mgrid]
      ,[fcst_sect_id]
      ,[fcst_year]
      ,[fcst_jan]
      ,[fcst_feb]
      ,[fcst_mar]
      ,[fcst_apr]
      ,[fcst_may]
      ,[fcst_jun]
      ,[fcst_jul]
      ,[fcst_aug]
      ,[fcst_sep]
      ,[fcst_oct]
      ,[fcst_nov]
      ,[fcst_dec]
      ,[fcst_comments]
  FROM [EMS].[dbo].[TEMSFCST]
  where fcst_mgrid='00809571' and fcst_year=2016 and 
  (
  fcst_jan not in (1,2,3,4,5,205,211) or 
  fcst_feb not in (1,2,3,4,5,205,211) or 
  fcst_mar not in (1,2,3,4,5,205,211) or 
  fcst_apr not in (1,2,3,4,5,205,211) or 
  fcst_may not in (1,2,3,4,5,205,211) or 
  fcst_jun not in (1,2,3,4,5,205,211) or
  fcst_jul not in (1,2,3,4,5,205,211) or 
  fcst_aug not in (1,2,3,4,5,205,211) or 
  fcst_sep not in (1,2,3,4,5,205,211) or
  fcst_oct not in (1,2,3,4,5,205,211) or 
  fcst_nov not in (1,2,3,4,5,205,211) or 
  fcst_dec not in (1,2,3,4,5,205,211)
  )

1 个答案:

答案 0 :(得分:0)

如果您想获得在任何月份列中没有这些值(1,2,3,4,5,205,211)的记录:

(
fcst_jan not in (1,2,3,4,5,205,211) or 
fcst_feb not in (1,2,3,4,5,205,211) or 
...
)

你应该改变'或''按'和',否则如果一条记录的fcst_jan = 25且fcst_feb = 205,它将出现在最终结果中。

(
fcst_jan not in (1,2,3,4,5,205,211) and
fcst_feb not in (1,2,3,4,5,205,211) and
...
)