复杂SQL where子句大于和小于

时间:2017-10-29 07:21:33

标签: sql sql-server sql-query-store

@fromValue FLOAT; @toValue FLOAT;
SELECT *
 FROM INVOICE
 WHERE TOTAL > @fromValue and TOTAL < @toValue

我有一个带有TOTAL列的INVOICE表。我想在这样的组合框中选择3个条件:

  • 总计&lt; 100 - &gt; (@fromValue = 0; @toValue = 100)
  • 100&lt;总计&lt; 200 - &gt; (@fromValue = 100; @toValue = 200)
  • 总计&gt; 500 - &gt; (@fromValue = 500; @toValue ???)

3 个答案:

答案 0 :(得分:1)

您可以传递以下变量:

DECLARE @fromValue DECIMAL(18,4), @toValue DECIMAL(18,4);

SET @fromValue = 500 ; -- depending on initial value
--SET @toValue = NULL;

SELECT *
FROM INVOICE
WHERE TOTAL BETWEEN ISNULL(@fromVALUE,0) AND ISNULL(@toValue,9999999);

答案 1 :(得分:0)

我一定错过了什么,因为它看起来太简单了?

SELECT 
    *
FROM
    Invoice
WHERE
    Total < 100 OR Total Between 100 and 200 OR Total > 500

答案 2 :(得分:0)

我无法想象或明白你需要什么,但如果你想传递两(2)个值并使用它,那么:

SELECT *
FROM INVOICE
WHERE TOTAL BETWEEN @fromValue AND @toValue;