2012-05-30 47 views
1

我有點困惑atm,我正在一個房地產網站上工作,需要一個小的搜索,問題是一些列表有一個價格範圍,例如從$ 10,000到$ 25,000,其他只是有一個固定的價格。SQL數據檢查和邏輯

所以我的數據庫看起來是這樣的:

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

搜索包括了2場:minPriceRange和maxPriceRange,例如,當在一個價格範圍內的用戶搜索的列表:minPriceRange = 8000和maxPriceRange = 17000,那麼列表1,2和3應該顯示出來。

我對如何在SQL語句中處理這個有點困惑。

所以它會檢查,如果價格是在最小和maxPrice之間,但也考慮到價格可以seperatly設置

編輯

我理解這可能是一個有點混亂。

所以基本上當我搜索12000到60000之間的列表時,列表1和3應該顯示出來。

這就是爲什麼我不能只是做minPrice> = 5000和maxPrice < = 60000

+0

2的價格是7500,爲什麼會認爲露面 - '當我搜索了12000個60000' –

+0

@rs之間的列表。對不起,我感到非常困惑,它應該顯示那個清單1和清單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)))) 
+0

再次,在某些情況下,這會給出問題,看到我的問題編輯 –

+0

我要把這個標記爲答案,因爲它幫助我解決了我的問題,非常感謝! –

0

如何根據價格範圍內選擇,然後用工會從固定價格加的結果?

一個pseude代碼嘗試爲客戶10,000和20,000尋找價格

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 
4
Select id from tablename where (minPrice >= 8000 and maxPrice <=17000) or (price between 8000 and 17000) 
+0

如果您在比較的某個字段爲空,這應該在SQL Server中工作嗎? –

+1

如果這三個字段是可空的呢? –

+2

@DavidStratton會將三個配對邏輯DBMS返回那些比較結果,並在哪個階段顯示未知結果? –

2

這可能是更好地爲你改變你的設計,取消價格欄,並用這些家庭設置價格,將最低價格和最高價格設置爲相同的價格。

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

根據其他需求,情況可能並非如此,但穩固的數據庫設計至關重要。我必須重寫的大多數應用程序都已經完成了,因爲之前的程序員(通常是之前的測試員是我)並沒有徹底思考數據庫設計,而是根據有缺陷的設計將它們放回了一個角落。

這就是說,與當前數據庫的設計,你可以使用括號,以便將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 
+1

那麼這將不起作用如果我使用8000到80000,那麼這是同樣的問題,即使應該選擇1,2和3。 –

+0

我的服務器可能不支持這個嗎?原生函數'ISNULL'調用中的參數計數不正確 –

+0

可能是。哪個RDBMS?我測試了我的SQL Server。 (接過來設置表,插入數據,並運行該查詢回答錯了前兩次後的時間。) – David

0
SELECT * FROM table 
WHERE 
    price BETWEEN :min AND :max 
OR (
    minPrice <= :min AND maxPrice >= :min 
) 
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 
+0

嗯,但不會這有和前面提到的一樣的問題嗎? 因此,假設我搜索5000到8000之間的任何東西,它只會選擇列表2 –

+0

您希望它返回5000和8000的值是什麼?你能清楚你的最終結果 –

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,它只是選擇每一個可能的結果

0

,我會寫這個查詢:

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

這樣可以使邏輯在一個地方,僅通過更換當前者不可用時,最小和最大值與價格。