2

我在 MySQL 中的 select 语句有问题。

我有两个陈述只是略有不同,第一个似乎有效,但第二个无效。第二条语句返回 0 个结果。我错过了什么/做错了什么?

设置变量

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

1# 之间没有

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

2#之间

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

在此处输入图像描述

4

2 回答 2

0

尝试在您的 SELECT 中包含 where 子句组件。IE:

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);
于 2012-10-25T14:37:56.980 回答
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);
于 2012-10-25T14:49:23.250 回答