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

MSSQL-在where子句中同时检查两列

  •  1
  • MavidDeyers  · 技术社区  · 7 年前

    我有以下oracle语法,它允许我在in语句中使用两列( Source )。遗憾的是,此语句与MSSQL不兼容:

    SELECT s.period, s.year, s.amount 
    FROM salaries s
    where (s.year, s.period) in (select year, period from periods)
    

    我通过连接两列找到了解决方案。然而,我想知道是否还有更专业的解决方案?

    SELECT s.period, s.year, s.amount 
    FROM salaries s
    where (s.year + ' ' + s.period) in (select year + ' ' + period from periods)
    
    3 回复  |  直到 7 年前
        1
  •  3
  •   PeterDeV    7 年前

    您可以使用WHERE EXISTS进行此操作。

    SELECT s.period, s.year, s.amount 
    FROM salaries s
    where exists (
        SELECT *
        from periods
        where year = s.year and period = s.period
    )
    
        2
  •  2
  •   Raymond W    7 年前

    在MS SQL中,可以使用where exists方法,这更有效。

    SELECT 
        s.period
        , s.year
        , s.amount 
    FROM salaries s
    where exists(
        select year, period from periods p where s.year = p.year and s.period = p.period
    )
    
        3
  •  1
  •   Dheerendra    7 年前

    SELECT DISTINCT s.[period]
          ,s.[year]
          ,s.amount 
    FROM salaries s
    CROSS APPLY
    (SELECT * 
     FROM [periods]
     WHERE [year] = s.[year]
     AND [period] = s.[period]
    )Res1