SELECT语句WHERE BETWEEN计算字段 - 空结果

时间:2012-10-25 14:32:09

标签: mysql

我遇到了MySQL中的select语句问题。

我有两个声明略有不同,第一个似乎有效但第二个没有。第二个语句返回0结果。我错过了什么/做错了什么?

设置变量

SET @maxOK = 1.2;
SET @minOK = .8;

1#NOT BETWEEN

SELECT *, fee - (receipts * -1) AS difference
FROM div.Data1
WHERE (receipts * -1) NOT BETWEEN (fee * @minOK) AND (fee * @maxOkay);

2#BETWEEN

SELECT *, fee - (receipts * -1) AS difference,
          receipts * -1 as rec,
          fee * @minOk as min, fee * @maxOk as max
FROM div.Data1
WHERE (receipts * -1) BETWEEN (fee * @minOK) AND (fee * @maxOkay);

一些数据应该显示在两个但不是

SELECT *, fee - (receipts * -1) AS difference,
          receipts * -1 as rec,
          fee * @minOk as min, fee * @maxOk as max
FROM div.Data1

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试在SELECT中包含where子句组件。  即:

SELECT (fee * @minOK) as wheremin, (fee * @maxOkay) as wheremax, *, fee - (receipts * -1) AS difference, receipts * -1 as rec, fee * @minOk as min, fee * @maxOk as max FROM div.Data1 WHERE (receipts * -1) BETWEEN (fee * @minOK) AND (fee * @maxOkay);

答案 1 :(得分:0)

您的陈述中有错误。您已声明@maxOk,但在@maxOkay子句中使用了BETWEEN

由于@maxOkay未在任何地方声明,因此它已初始化为NULL,因此您的陈述基本归结为

SET @maxOK = 1.2;
SET @minOK = .8;

SELECT *
       , fee - (receipts * -1) AS difference
       , receipts * -1 as rec
       , fee * @minOk as min
       , fee * @maxOk as max
FROM Data1
WHERE (receipts * -1) BETWEEN (fee * @minOK) AND NULL

语句应

SET @maxOK = 1.2;
SET @minOK = .8;

SELECT *
       , fee - (receipts * -1) AS difference
       , receipts * -1 as rec
       , fee * @minOk as min
       , fee * @maxOk as max
FROM Data1
WHERE (receipts * -1) BETWEEN (fee * @minOK) AND (fee * @maxOK);