SQL数据检查和逻辑

时间:2012-05-30 13:58:47

标签: sql conditional-statements

我有点困惑atm,我正在开发一个需要小搜索的房地产网站,问题是一些房源有价格范围,例如从10,000美元到25,000美元,其他只有固定价格

所以我的数据库看起来像这样:

id | price | minPrice | maxPrice
1  |       | 10000    | 45000
2  | 7500  |          |
3  |       | 15000    | 20000
4  | 80000 |          |

搜索由2个字段组成:minPriceRange和maxPriceRange,例如,当用户搜索价格范围中的列表时:minPriceRange = 8000和maxPriceRange = 17000,然后列出1,2和3应显示。< / p>

我对如何在SQL语句中处理这个问题感到困惑。

所以它必须检查价格是否介于最小和最大价格之间,还要考虑价格是否可以单独设定

修改

我知道这可能有点令人困惑。

所以基本上当我搜索12000到60000之间的列表时,列表1和3应该显示出来。

这就是为什么我不能只做minPrice&gt; = 5000 AND maxPrice&lt; = 60000。

8 个答案:

答案 0 :(得分:4)

Select id from tablename where (minPrice >= 8000 and maxPrice <=17000) or (price between 8000 and 17000)

答案 1 :(得分:2)

您可能最好更改设计以消除价格列,对于那些设定价格的房屋,请将最低价格和最高价格设置为相同的价格。

id | minPrice | maxPrice
1  | 10000    | 45000
2  | 7500     | 7500
3  | 15000    | 20000
4  | 80000    | 80000

根据其他需求可能不是这种情况,但坚实的数据库设计至关重要。我必须重写的大多数应用程序都已完成,因为一些以前的程序员(通常是以前的probgrammer就是我)没有仔细考虑数据库设计,而是基于有缺陷的设计进入了一个角落。

也就是说,使用当前的数据库设计,您可以使用括号对where子句进行分组 (信用到期时,此查询的99%来自rs。我必须修改一个项目才能使其正常工作。)

DECLARE @pricemin int = 8000 
DECLARE @pricemax int = 17000 
SELECT * FROM tableName WHERE (ISNULL(minPrice,0) <= @pricemin  
AND    @pricemax <= ISNULL(maxPrice,2147483647))   -- 2147483647 is max Int Value in SQL Server - adjust as necessary
or ISNULL(price,0) between @pricemin and @pricemax 

答案 2 :(得分:2)

这应该有效:[更新]

DECLARE @table table (id int, price int, minPrice int, maxPrice int)
INSERT into @table 
SELECT 1, null, 10000, 450000 UNION
SELECT 2,7500,null,null UNION
SELECT 3,null,15000  ,20000 UNION
SELECT 4,80000, null,null    

DECLARE @pricemin int = 8000
DECLARE @pricemax int = 17000
SELECT * FROM @table WHERE (minPrice <= @pricemin or minPrice is null) 
AND    (@pricemax <= maxPrice or maxPrice is null) 
or ISNULL(price,0) between @pricemin and @pricemax

答案 3 :(得分:2)

DECLARE @table table (id int, price int, minPrice int, maxPrice int)
INSERT into @table 
SELECT 1, null, 10000, 450000 UNION
SELECT 2,7500,null,null UNION
SELECT 3,null,15000  ,20000 UNION
SELECT 4,80000, null,null  

DECLARE @pricemin int = 15000
DECLARE @pricemax int = 90000

SELECT * FROM @table
WHERE (((minPrice IS NOT NULL AND minPrice <= @pricemin)
   AND (maxPRICE IS NOT NULL AND maxPrice >= @pricemax))
   OR
   (price IS NOT NULL AND (( price >= @pricemin) AND (price <= @pricemax))))

答案 4 :(得分:0)

如何根据价格范围进行选择,然后使用工会来添加固定价格的结果?

针对寻找10,000到20,000之间价格的客户的pseude代码尝试

SELECT id FROM table WHERE price BETWEEN 10,000 AND 20,000
UNION
SELECT id FROM table WHERE minPrice > 10,000 AND maxprice < 20,000

答案 5 :(得分:0)

SELECT * FROM table
WHERE
    price BETWEEN :min AND :max
OR (
    minPrice <= :min AND maxPrice >= :min
)

答案 6 :(得分:0)

我会把它作为一个新的答案发布,让它不那么令人困惑,再次打破我的头脑后,我终于想到了这个:

(((minPrice IS NOT NULL AND minPrice >= 10000) AND (minPrice IS NOT NULL AND minPrice <= 15000)) 
OR 
((maxPrice IS NOT NULL AND maxPrice >= 10000) AND (maxPrice IS NOT NULL AND maxPrice <= 15000))) 
OR
(price IS NOT NULL AND (( price >= 10000) AND (price <= 15000)))

现在这项工作适用于您选择最低和最高价格的情况,但如果最低价格为0,则只选择每个可能的结果

答案 7 :(得分:0)

我会将此查询编写为:

select *
from t
where <usermin> >= coalesce(minprice, price) and
      <usermax> <= coalesce(maxprice, price)

这可以将逻辑保存在一个地方,只需将前一个不可用的价格替换为最小值和最大值。