代码之家  ›  专栏  ›  技术社区  ›  Faruz

发生“CASE ELSE”时从SQL中删除行

  •  1
  • Faruz  · 技术社区  · 14 年前

    这是我的sql查询:

    select case when table.field1 = 1 then 1
                when table.field2 = 3 then 3
                when table.field2 = 4 then 4
                when table.field3 = 1 then 5
                else .... Status //item name
    from table
    

    我希望在出现“else”的情况下->该行将从数据集中删除。

    思想?

    2 回复  |  直到 14 年前
        1
  •  5
  •   Jeff Mattfield Jeff Mattfield    14 年前

    with TempResult (id, status)
    as
    (
        select primary_key_column,
               case when table.field1 = 1 then 1
                    when table.field2 = 3 then 3
                    when table.field2 = 4 then 4
                    when table.field3 = 1 then 5
                    else 0
        from table
    )
    
    select id
    from TempResult
    where status > 0
    
        2
  •  0
  •   spbfox    14 年前

    最严格的方法是从“when”开始为字段设置条件,即field1=1或field2 in(3,4)或field3=1