【问题标题】:How do I combine conditions in where clause for two columns in SQL Server如何在 SQL Server 中的两列的 where 子句中组合条件
【发布时间】:2019-12-18 22:15:55
【问题描述】:

我有一个简单的表 numInfo,其中包含两个整数列 col1col2。现在我想将它们与一个数字进行比较,所以我会写一个这样的查询:

select * from numInfo where col1 < 26 OR col2 <26

是否可以将这两个条件组合为:

select * from numInfo where (col1 or col2) < 26

上面的查询给了我一个错误,所以我只想知道如何将条件组合成一个?

【问题讨论】:

    标签: sql sql-server database where-clause


    【解决方案1】:

    不能将&lt; 的条件组合成简洁的表达式。

    如果是=,您可以使用in。对于&lt;&gt;,您可以使用not in&lt; 没有对应的快捷方式。你甚至不能使用least()/greatest(),因为 SQL Server 不支持这些功能。

    【讨论】:

      【解决方案2】:

      你需要:

      select * from numInfo where (col1 < 26 or col2 < 26);
      

      【讨论】:

      • 这没有帮助,我不想在我的条件下把数字 26 写两次。
      • 无论如何,您都需要遵守语言要求。
      猜你喜欢
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多