Mysql 按自定义条件对搜索结果进行排序
我会尽可能多地解释我的问题 我有一个搜索表单,用户可以选择不同的参数并输入不同的值来搜索属性。就像我有一个表Mysql 按自定义条件对搜索结果进行排序,mysql,sql,Mysql,Sql,我会尽可能多地解释我的问题 我有一个搜索表单,用户可以选择不同的参数并输入不同的值来搜索属性。就像我有一个表tbl\u properties 在搜索表单中,用户选择,property\u category,property\u type[租赁、出售或租赁],输入价格范围,无卧室,位置,地区,属性可能位于tbl\u属性或其他查找表中。编写查询不是我的问题,但我遇到的问题是,我想在搜索结果中按以下模式对记录进行排序 首次显示结果-按降序匹配位置的价格 然后展示与该地区相匹配的价格 然后显示-同一地区
tbl\u properties
在搜索表单中,用户选择,property\u category
,property\u type
[租赁、出售或租赁],输入价格范围,无卧室
,位置
,地区
,属性可能位于tbl\u属性
或其他查找表中。编写查询不是我的问题,但我遇到的问题是,我想在搜索结果中按以下模式对记录进行排序
首次显示结果-按降序匹配位置的价格
然后展示与该地区相匹配的价格
然后显示-同一地区的卧室数量匹配
然后显示-价格/卧室/物业面积匹配但在不同地区
我只是想提示一下,这些记录是如何按这些顺序排序的
编辑
下面是我对表结构的简要描述
tbl_properties
-------------------
property_id INT
category_id INT
property_name VARCHAR
price INT
district_id INT
location VARCHAR
property_type ENUM('lease','sale','rent')
tbl_category
-------------
category_id INT
category_name VARCHAR
tbl_districts
-----------------
district_id INT
district_name VARCHAR
tbl_property_details
------------------------
detail_id INT
property_id INT
no_of_bedrooms INT
property_area DECIMAL
感谢从tbl\U属性中选择属性类别属性类型,其中按排序
所以。。。按价格说明订购
等等
这里有一个很好的链接,可以按案例使用
当价格介于@priceMin和@priceMax之间时
位置=@location然后是1
当价格介于@priceMin和@priceMax之间时
而district_id=@districtid然后是2
当没有卧室时=@没有卧室
districtid=@districtid然后是3
当价格介于@priceMin和@priceMax之间时
没有卧室=@没有卧室
和property_area=@property_area和districtid@districtid,然后是4
其他5
完,价格说明
您确定是指价格的降序吗?如中所示,最高价格优先?@RedFilter,请查看我的编辑。这不是我想要的,请仔细阅读我的问题。
order by case
when price between @priceMin and @priceMax
and location = @location then 1
when price between @priceMin and @priceMax
and district_id = @districtid then 2
when no_of_bedrooms = @no_of_bedrooms
and districtid = @districtid then 3
when price between @priceMin and @priceMax
and no_of_bedrooms = @no_of_bedrooms
and property_area = @property_area and districtid <> @districtid then 4
else 5
end, price desc