对于我的Realty网站,我还想从两个下拉列表MinPrice
和MaxPrice
(也是字段值)返回属于用户请求的价格范围内的属性。
以下是我的发言。除上述要求外,它的工作正常。
SELECT busname, email, render_pic,
area,logo, url, email, map,
description, tag, catch_phrase, region
FROM Results
WHERE STYLE LIKE 'varStyle'
AND REGION LIKE 'varRegion'
AND BEDROOMS LIKE 'varBedrooms'
AND BATHROOMS LIKE 'varBathrooms'
ORDER BY ID desc
提前谢谢你!
答案 0 :(得分:2)
and Price between @minprice and @maxprice
编辑:要涵盖@paxdiablo的建议,虽然我不知道为什么房子除了价格之外还有什么东西......
and (minprice between @minprice and @maxprice
or maxprice between @minprice and @maxprice
or @minprice between minprice and maxprice)
答案 1 :(得分:0)
有两个重叠的区域(在输入和记录中)有点嘀嗒,但你应该尝试:
SELECT
busname, ...
FROM Results
WHERE STYLE LIKE &varStyle
AND REGION LIKE &varRegion
AND BEDROOMS LIKE &varBedrooms
AND BATHROOMS LIKE &varBathrooms
AND (
MINPRICE between &MinPrice and &MaxPrice
OR MAXPRICE between &MinPrice and &MaxPrice
OR (MINPRICE <= &MinPrice AND MAXPRICE >= &MaxPrice)
)
ORDER BY ID desc
如果您的行最小/最大范围内的任何价格都在您的搜索最小/最大范围内(包括行最小/最大完全在搜索范围内)或整个搜索范围在您的行范围内,这将匹配是我认为你想要的。
此外,您可能需要重新考虑使用LIKE
,除非实际允许那些var...
变量拥有通配符。一些不太智能的DBMS可能无法自动将它们转换为更高效的=
操作。
答案 2 :(得分:0)
SELECT busname, email, render_pic,
area,logo, url, email, map,
description, tag, catch_phrase, region
FROM Results
WHERE STYLE LIKE 'varStyle'
AND REGION LIKE 'varRegion'
AND BEDROOMS LIKE 'varBedrooms'
AND BATHROOMS LIKE 'varBathrooms'
AND Price between varMinPrice and varMaxPrice
ORDER BY ID desc
答案 3 :(得分:0)
这就是我执行此类查询的方式:
只传递用户选择的任何其他未指定的值,因为它们在下面的sproc中默认为NULL。我猜对了参数类型,但你明白了。还添加了'%',以便区域和样式是通配的。
以下查询将根据所提供的参数的任何或所有组合返回匹配结果。
CREATE PROCEDURE [dbo].[spGetProperties]
@style VARCHAR(50) = NULL
,@region VARCHAR(50) = NULL
,@bedrooms TINYINT = NULL
,@bathrooms TINYINT = NULL
,@minPrice INT = NULL
,@maxPrice INT = NULL
AS
BEGIN
SELECT busname, email, render_pic,
area,logo, url, email, map,
description, tag, catch_phrase, region
FROM Results
WHERE (@style IS NULL OR STYLE LIKE '%' + @style + '%')
AND (@region IS NULL OR REGION LIKE '%' + @region + '%')
AND (@bedrooms IS NULL OR BEDROOMS >= @bedrooms)
AND (@bathrooms IS NULL OR BATHROOMS >= @bathrooms)
AND (@minPrice IS NULL OR MINPRICE >= @minPrice)
AND (@maxPrice IS NULL OR MAXPRICE <= @maxPrice)
ORDER BY ID desc
END