0

我正在寻找一种解决方案,我可以直接在我的 sql 语句中根据字段值获取结果。

例如,如果我有以下结构:

calculated_result     result_one     result_two     result_three
1                     2.50           3.40           2.90
2                     1.90           2.00           3.90
1                     1.30           2.23           1.50

然后我想进行搜索,它应该返回所有计算结果为 1 且 result_one 高于 2.30 且小于或等于 2.65 的结果。

问题是,我不知道如何只返回适合用户搜索的行。

computed_result = 1 应该检查 result_one 是否大于和小于行内容计算结果 = 2 应该检查 result_two 是否大于和小于行内容等

希望这是有道理的。

4

4 回答 4

4
select  *
from    myTable
where   (calculated_result = 1 and result_one between 2.30 and 2.650)
or      (calculated_result = 2 and result_two between 2.30 and 2.650)
or      (calculated_result = 3 and result_three between 2.30 and 2.650)

或者,如果您想使用case

select  *
from    myTable
where   case 
          when calculated_result = 1 then result_one 
          when calculated_result = 2 then result_two
          when calculated_result = 3 then result_three
        end between 2.30 and 2.650
于 2013-02-01T18:30:18.343 回答
1

您可以尝试使用CASE运算符

SELECT * FROM yourTable
WHERE (CASE
  WHEN calculated_result = 1 THEN result_one
  WHEN calculated_result = 2 THEN result_two
END) > 2.3
AND (CASE
  WHEN calculated_result = 1 THEN result_one
  WHEN calculated_result = 2 THEN result_two
END) <= 2.65
于 2013-02-01T18:35:27.960 回答
0

尝试这个

SELECT 
       * 
FROM 
       table_name 
WHERE 
       calculated_result = 1 AND
       result_one > 2.30 AND
       result_one <= 2.65 
于 2013-02-01T18:30:09.063 回答
0

其他:

select  *
from    myTable
where   calculated_result in (1, 2, 3) and 
(result_one between 2.30 and 2.650 or result_two between 2.30 and 2.650
or result_three between 2.30 and 2.650)
;

演示

于 2013-02-01T18:33:26.513 回答